Developer Tools
Gray code converter.
Convert binary or decimal to reflected Gray code, or decode Gray code back to binary, up to 64 bits. See the XOR working and a full reflected Gray table for 1 to 8 bits. Runs in your browser.
Input
Result
Enter a value, then press Convert.
Reflected Gray table
How it works
- Binary to Gray: gray = b XOR (b >> 1). Shift the binary number right one place and XOR it with the original. The top bit is unchanged.
- Gray to binary: the binary bits are the running (prefix) XOR of the Gray bits from the most significant bit down. Example: Gray 1110 gives 1, 1^1=0, 0^1=1, 1^0=1, so binary 1011.
- Hamming-distance-1 property: in the reflected code, consecutive values differ in exactly one bit, and the last value differs from the first by one bit too. Ordinary binary can flip many bits at once (0111 to 1000 flips four), which is why Gray code is used for position encoders where a misread mid-transition would give a large error.
- Origin: Frank Gray of Bell Labs described the reflected binary code in US patent 2,632,058, "Pulse code communication", filed 13 November 1947 and granted 17 March 1953.
- Scope: this is the standard reflected binary Gray code. Other Gray-type codes exist and are not produced here. Uses BigInt, so 64-bit values are exact. Nothing is sent anywhere.
Sources: US 2,632,058, Pulse code communication (Google Patents); Wikipedia, Gray code. Related: Two's Complement Calculator.
Worked example
Computed by running this page’s converter code on 2026-10-26.
Binary 1011 (decimal 11) becomes Gray 1110 (hex E); the working shown is binary = 1011, b >> 1 = 0101, XOR = 1110 (gray). Gray 1110 decodes back to 1011.
| Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| 3-bit Gray | 000 | 001 | 011 | 010 | 110 | 111 | 101 | 100 |
Each step in the sequence changes exactly one bit, and the last value 100 differs from the first 000 by one bit as well.
Sources and as-of date
As of 2026-10-26. The code follows the reflected binary construction in the original patent; the XOR-and-shift form uses the standard JavaScript operators.
- US patent 2,632,058, Pulse code communication (F. Gray) (retrieved 2026-10-26): the original description of the reflected binary code.
- MDN: Bitwise XOR (^) (retrieved 2026-10-26): XOR on integer operands.
- MDN: Right shift (>>) (retrieved 2026-10-26): the sign bit is copied into the gap.
Frequently asked questions
How do you convert binary to Gray code?
XOR the number with itself shifted right by one place. For 1011: b >> 1 is 0101, and 1011 XOR 0101 is 1110.
How do you convert Gray code back to binary?
Keep the top bit, then XOR each following Gray bit with the binary bit just produced. Gray 1110 gives 1011.
Why is Gray code used in position encoders?
Consecutive Gray values differ in exactly one bit, so a reading taken mid-transition is off by at most one step. Plain binary can flip many bits at once: 7 to 8 (0111 to 1000) flips 4.
Is this the only Gray code?
No. Other Gray-type codes exist. This page produces the standard reflected binary Gray code described in Frank Gray’s 1953 patent.