Convert gray code to binary number with step-by-step conversion process. See the detailed transformation from Gray code to standard binary representation.
Gray Code to Binary Converter
Convert Gray code to Binary or Binary to Gray code with step-by-step XOR explanation and truth table
Quick Examples:
Enter a binary string (only 0s and 1s)
Gray code is a way of ordering binary numbers so that only one bit changes between any two numbers that sit next to each other in the sequence. It was formalized by Frank Gray at Bell Labs and patented in 1953, originally to solve a very physical problem in early switching and signal equipment. The name reflected binary code describes the same thing and shows up just as often in textbooks.
This converter takes a Gray code value and returns the equivalent standard binary number, along with the bit-by-bit logic used to get there, so you can follow exactly how each output bit was produced rather than trusting a black box.
What Gray Code Is and the Problem It Solves
Standard binary counting can flip several bits at once between consecutive values. Moving from 3 to 4 changes 011 to 100, which is every single bit at once. In software that jump is instantaneous and harmless. In physical hardware, like a mechanical rotary sensor or an old-style electromechanical switch, those bit changes rarely happen at the exact same instant. For a brief moment the sensor can report a completely wrong intermediate value, a glitch that has nothing to do with the actual position being measured.
Gray code eliminates that risk by guaranteeing exactly one bit changes at every step in the sequence. Even if the hardware reads the bits at a slightly awkward moment, at most one bit is ambiguous, so the reading is never wildly wrong.
How Gray to Binary Conversion Works
The conversion works from the most significant bit down to the least significant bit. The top bit of the binary result always matches the top bit of the Gray code exactly, no calculation needed. Every bit after that is found by taking an exclusive OR, or XOR, between the binary bit you just calculated and the next Gray code bit in line.
In plain terms: the first bit copies straight across, and every following bit is the previous binary bit XORed with the corresponding Gray bit. This chain of XOR operations, one per bit, is the entire algorithm.
Step by Step Usage
- Enter the Gray code value you want to convert, using binary digits only.
- Confirm the bit length matches what you intend to convert, since leading zeros matter.
- Click Convert.
- Read the resulting binary value along with the XOR chain used to build it.
Worked Examples
Example 1: Converting Gray Code 1011
The top bit copies straight across: binary bit 3 equals Gray bit 3, which is 1.
Binary bit 2 = binary bit 3 XOR Gray bit 2 = 1 XOR 0 = 1.
Binary bit 1 = binary bit 2 XOR Gray bit 1 = 1 XOR 1 = 0.
Binary bit 0 = binary bit 1 XOR Gray bit 0 = 0 XOR 1 = 1.
Result: 1101 in standard binary, which is 13 in decimal.
Example 2: Converting Gray Code 1000
Top bit copies across: 1.
Next bit: 1 XOR 0 = 1.
Next bit: 1 XOR 0 = 1.
Final bit: 1 XOR 0 = 1.
Result: 1111 in binary, which is 15 in decimal. This all-ones pattern is a useful edge case to remember, since it always appears at the far end of a Gray code sequence.
Example 3: Converting a 3-bit Gray Code 101
Top bit copies across: 1.
Next bit: 1 XOR 0 = 1.
Final bit: 1 XOR 1 = 0.
Result: 110 in binary, which is 6 in decimal.
Where Gray Code Is Actually Used
- Rotary encoders and shaft position sensors in robotics and industrial automation, where single-bit transitions prevent false position readings during movement.
- Karnaugh maps in digital logic design. Rows and columns are ordered in Gray code specifically so that any two adjacent cells differ by only one variable, which is what makes visual grouping and simplification possible.
- Genetic algorithms, where representing candidate solutions in Gray code avoids the Hamming cliff problem: in standard binary, values that are numerically close can differ by many bits, which confuses mutation and crossover operations.
- Analog-to-digital converters that use Gray code internally between conversion stages to reduce the chance of a multi-bit transition error during sampling.
- Early telegraphy and signal encoding systems, which is where Gray’s original patent work was aimed.
Common Mistakes When Converting Gray Code
- Running the XOR chain from the least significant bit instead of the most significant bit. The direction matters and reversing it gives a wrong answer.
- Forgetting that the top bit never changes between Gray code and binary. It is a direct copy, not a calculation.
- Confusing this conversion with the reverse process. Binary to Gray uses a different formula: each Gray bit is the XOR of a binary bit and the binary bit directly above it.
- Losing track of leading zeros, which changes both the bit length and the resulting value if handled inconsistently.
Tips for Accurate Conversions
- Write the bits out vertically, most significant bit first, before starting the XOR chain, rather than trying to do it in your head.
- For small values, cross-check the result against a short Gray code reference table for numbers under 16.
- Remember that any two consecutive Gray code values in a sequence differ by exactly one bit. If your conversion of consecutive inputs shows more than one bit changing, recheck your work.
Gray Code vs Standard Binary
| 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 |
Frequently Asked Questions
Who invented Gray code and when?
Frank Gray, a researcher at Bell Labs, patented the technique in 1953, though similar reflected binary sequences had appeared in earlier telegraphy work before being formalized under his name.
Is Gray code the same as unit-distance code?
Yes, unit-distance code is simply another name for the same idea: a sequence where every consecutive pair of values differs by exactly one bit.
Can Gray code represent negative numbers or fractions?
Gray code is a way of ordering and encoding bit patterns, not a numeric format in itself, so negative numbers and fractions are not directly represented. Any signed or fractional handling has to be layered on top by the surrounding system.
Why not just use binary directly in rotary encoders?
Because standard binary can flip multiple bits at the same transition point. If the hardware reads those bits even a fraction of a second out of sync, the sensor can briefly report a value nowhere near the real position. Gray code limits any such misread to a single bit.
How do you convert binary back to Gray code?
Each Gray bit is the XOR of the corresponding binary bit and the binary bit one position higher. The most significant bit is again copied across unchanged, and every bit after that comes from XORing a binary bit with its neighbor above it.
Conclusion
Gray code solves a specific, physical problem: keeping bit transitions clean when timing can’t be perfectly guaranteed. Once you see the XOR chain that ties Gray code back to standard binary, the conversion stops feeling like a trick and starts feeling like straightforward bookkeeping. Use the converter above whenever you need to move between the two representations, whether that’s for a rotary encoder project, a Karnaugh map, or a systems course assignment.