Counting in binary has a hidden hazard: stepping from 7 to 8 rewrites the number from 0111 to 1000, four bits changing in one tick. In software the change is instant; in hardware, where four physical switches never flip at precisely the same moment, a sensor read mid-transition can capture a mixture, and a volume knob between 7 and 8 might briefly report 15. Gray code is the cure: a reordering of binary where every step changes exactly one bit, so there is never a mixture to misread. This guide covers how it works, the one-line conversion trick, and where it quietly runs your hardware, with our free binary to Gray code converter for trying it live.
In this guide
The multi-bit flip problem
Ordinary binary counting flips a varying number of bits per step: 2 to 3 touches one bit, 3 to 4 touches three, 7 to 8 touches four. A device reading a mechanical or optical encoder during such a transition sees each bit settle independently, so the captured value can be any mixture of the old and new patterns, briefly and convincingly wrong by a large amount. The errors are worst exactly at the “round number” boundaries where the most bits move, and no amount of better engineering removes the race entirely, because the race is in the representation, not the parts. Changing the representation removes it.
The sequence: 0 to 7, one bit at a time
| Decimal | Binary | Gray |
|---|---|---|
| 0 | 000 | 000 |
| 1 | 001 | 001 |
| 2 | 010 | 011 |
| 3 | 011 | 010 |
| 4 | 100 | 110 |
| 5 | 101 | 111 |
| 6 | 110 | 101 |
| 7 | 111 | 100 |
Read down the Gray column: every neighboring pair differs in exactly one position, including the dangerous 3-to-4 crossing, which is now just 010 to 110, one bit. The pattern even closes its own loop: 7 back to 0 is 100 to 000, also one bit, so a rotating dial that wraps around has no bad position anywhere on the circle. The cost is that Gray values no longer read as quantities, 110 sits in the row for 4, which is why Gray code is a transport and measurement format, converted back to binary the moment arithmetic is needed.
Why “reflected”: the mirror construction
The full name is reflected binary code, and the construction explains it. Start with the 1-bit sequence 0, 1. To double it: write the sequence forward, then write it again backward (the reflection), prefix the forward half with 0 and the mirrored half with 1. From 0, 1 comes 00, 01, 11, 10; reflect again for the 3-bit table above. The mirroring is what guarantees the one-bit property at every scale: within each half only the old code changes (one bit by induction), and at the seam between halves the sequence meets its own reflection, identical except for the new front bit. The same construction is why the sequence wraps cleanly, the last value being the first value’s mirror image plus the leading 1.
The one-line conversion, both directions
For all its elegance, the conversion to Gray is one XOR: shift the binary value right by one bit and XOR it with itself. Hex A5, 1010 0101 in binary, shifted gives 0101 0010, and the XOR yields 1111 0111, hex F7, its Gray form. The XOR marks exactly the positions where a bit differs from its left neighbor, which is precisely the information Gray code encodes. The return trip is a running XOR from the top bit downward, each output bit folding in one more input bit, mechanical in hardware and two lines in code. In the toolbox, hex to Gray and Gray to hex run the trip in both directions, Gray to decimal lands straight in everyday numbers, and the XOR doing all the work is the same difference-detector covered in the bitwise guide.
Where Gray code earns its keep
Rotary and absolute position encoders are the headline use: the encoder disc inside knobs, servo motors and robot joints is printed in Gray code so any reading mid-rotation is off by at most one step, never a wild mixture. Analog-to-digital boundary handling uses the same logic where a measured value hovers between two codes. Karnaugh maps, the logic-design tool, order their rows and columns in Gray code so adjacent cells differ by one variable, which is what makes the visual grouping trick work. And error-sensitive channels use Gray ordering so that the most likely corruption, one flipped bit, moves a value by the smallest possible amount instead of teleporting it. The common principle across all four: when the world can only be trusted one bit at a time, encode so one bit is all that ever changes. The broader family of bases and encodings lives in the number systems pillar.
Frequently asked questions
Is Gray code still binary?
Yes, in the sense that it uses only 0s and 1s; what changes is which pattern means which quantity. It is best thought of as binary with a different dictionary, optimized so neighboring quantities get neighboring patterns one bit apart.
Can you do arithmetic directly on Gray code values?
Not practically: place values do not mean fixed amounts, so addition has no column-by-column rule. The standard workflow is convert to binary, compute, convert back, and since each conversion is a couple of XOR operations, the round trip costs almost nothing.
Is there only one Gray code?
The reflected code in this guide is the standard one, but any ordering where neighbors differ by one bit qualifies, and specialized variants exist for particular hardware. When a datasheet says Gray code without qualification, it means the reflected construction here.
Why do the tools offer Gray conversions for hex and octal too?
Convenience of reading, not a different code: hardware registers and datasheets print values in hex, so converting hex to Gray directly skips a manual stop in binary. Underneath, every one of those conversions passes through the same bits and the same XOR.