What this tool does
This converter translates between ordinary integers and Gray code (binary-reflected code). In Gray code, every step from one number to the next flips exactly one bit, which is why it is the standard encoding for rotary and linear position sensors, and why it makes Karnaugh-map adjacency work.
How it works
Encoding an integer n to Gray code is a one-liner:
gray = n ^ (n >> 1)
The top bit of the Gray code equals the top bit of the binary, and each lower Gray bit is the XOR of two neighbouring binary bits. That XOR is exactly what cancels out all but one bit-change between consecutive numbers.
Decoding a Gray value back to a plain integer accumulates XORs from the most-significant bit downward:
binary = gray;
while (gray >>= 1) {
binary ^= gray;
}
Each binary bit is the running XOR of every Gray bit at or above its position.
Sequence comparison — binary vs Gray code
Seeing both sequences side by side makes the single-bit property obvious:
| Decimal | Binary | Gray code | Bits that changed |
|---|---|---|---|
| 0 | 000 | 000 | — |
| 1 | 001 | 001 | bit 0 |
| 2 | 010 | 011 | bit 1 |
| 3 | 011 | 010 | bit 0 |
| 4 | 100 | 110 | bit 2 |
| 5 | 101 | 111 | bit 0 |
| 6 | 110 | 101 | bit 1 |
| 7 | 111 | 100 | bit 0 |
In standard binary, going from 3 (011) to 4 (100) changes all three bits simultaneously. In Gray code the same transition — from 010 to 110 — changes only bit 2. That single-bit guarantee is what makes Gray code indispensable for hardware.
Why Gray code matters in practice
Rotary encoders are the most common application. A mechanical or optical rotary encoder reads position as a bit pattern from a disk with marked tracks. If the encoder uses standard binary, crossing a boundary between 3 and 4 requires three tracks to switch simultaneously. Mechanical tolerances mean the tracks never switch at exactly the same instant, so there is a brief window where the encoder can read any of several wrong values — for example, 111 (=7) instead of 4. Gray code eliminates this entirely because only one track ever changes per position step, leaving no ambiguous intermediate state.
Karnaugh maps (K-maps) use Gray-code ordering for row and column headers so that adjacent cells in the K-map correspond to logical terms that differ in exactly one variable. This is what makes grouping adjacent 1s in a K-map equivalent to simplifying boolean expressions — the Gray-code adjacency property directly enables boolean minimisation.
Error detection and digital communications: transitions that change only one bit are less likely to be misinterpreted as a three-level change, making Gray code useful in some analog-to-digital converter designs where comparator outputs are compared sequentially.
Worked example
The integer 5 is 101 in binary. Gray code: 101 ^ 010 = 111. The integer 6 is 110, Gray code: 110 ^ 011 = 101. They differ in exactly one bit (bit 1), confirming the property.
Notes
- The tool pads both the binary and Gray strings to the same width so the single-bit difference is easy to spot.
- Decoding ignores anything that is not
0or1; encoding requires a non-negative decimal integer. - JavaScript’s 32-bit bitwise operators cap reliable operation at 2,147,483,647 (2³¹ − 1).