Binary to Gray Code Converter
Convert binary code to Gray code, or decode Gray code back to binary, instantly. Switch direction below — no signup, completely free.
Visualize how binary is converted to Gray code using XOR gates. Click an input bit to toggle it, or enter a 4-digit value below.
How Gray Code Conversion Works
Gray code is a binary numeral system where two successive values differ in only one bit. To convert binary to Gray code:
1. The most significant bit (MSB) of Gray code is the same as the MSB of binary.
2. Each subsequent Gray code bit is obtained by XORing the current binary bit with the previous binary bit.
This circuit demonstrates the conversion process using XOR gates.
What is Gray Code?
Gray code — also called reflected binary code — is a way of ordering binary values so that only one bit changes between any two consecutive numbers. Standard binary doesn’t have that property: going from 7 (0111) to 8 (1000) flips all four bits at once, even though the values are only one apart.
That single-bit-change rule matters whenever a binary value is being read mechanically or electrically, not just calculated in software. Picture a rotary position sensor: if every bit could flip at the same instant, the tiniest timing mismatch between bits could make the sensor briefly report a wildly wrong position. Gray code removes that risk, since only one bit is ever in transition at a time.
It’s named after Frank Gray, a Bell Labs researcher who patented the technique in the 1950s for reliable signal transmission. The most common version — and the one this tool uses — is the binary-reflected Gray code, though balanced and other variants exist for specialized applications.
How to Convert Binary to Gray Code (and Back)
The whole conversion is built on the XOR operation. Here’s the logic for each direction:
Binary → Gray Code
- The most significant bit (MSB, the leftmost bit) stays exactly the same.
- Every bit after that is the current binary bit XORed with the binary bit to its left.
Example: Convert binary 1011 (decimal 11) to Gray code.
MSB: 1 → stays 1
Next: 0 XOR 1 = 1
Next: 1 XOR 0 = 1
Next: 1 XOR 1 = 0
Result: Gray code 1110
Gray Code → Binary
- The MSB stays the same.
- Each following binary bit is the current Gray bit XORed with the previous binary bit you just calculated (not the previous Gray bit).
Example: Convert Gray code 1110 back to binary.
MSB: 1 → stays 1
Next: 1 XOR 1 = 0
Next: 1 XOR 0 = 1
Next: 0 XOR 1 = 1
Result: Binary 1011
This tool handles up to 16 bits automatically, with output formatting options for spacing or grouping by four. The circuit visualizer further down the page shows the same logic with live XOR gates — toggle the input bits and watch the outputs update in real time.
4-Bit Binary to Gray Code Reference Table
Here’s the full conversion table for all 16 four-bit values. Notice that adjacent rows always differ by exactly one bit in the Gray code column — that’s the property the whole system is built around.
| Decimal | Binary | Gray Code |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Where Gray Code Shows Up in Real Life?
Gray code isn’t just a textbook exercise — it shows up anywhere reliability matters more than raw simplicity:
- Rotary encoders — volume knobs, robot joints, and industrial position sensors use Gray code so a misread never causes a huge jump in reported position.
- Karnaugh maps (K-maps) — digital logic design uses Gray-code ordering to simplify Boolean expressions and minimize circuits.
- Analog-to-digital converters (ADCs) — Gray-coded outputs reduce switching noise during signal conversion.
- Error detection in communications — keeping a Hamming distance of 1 between neighboring values limits how far a single bit error can throw off a transmitted value.
- Combinatorial algorithms — Gray code sequences are a common building block for generating combinations and permutations efficiently in code.
If you’re studying this for a course, the key concept to connect it to is Hamming distance — Gray code guarantees a distance of exactly 1 between consecutive values, which is precisely why it’s the standard choice for fault-tolerant systems.
Frequently Asked Questions
⚡ Try Other Binary Tools ⚡
Explore more converters and tools on Binary Code Converter.
