Gray code converter
Convert either way between binary and Gray code, and read off the full sequence for any width. Consecutive Gray values always differ in exactly one bit. Its formal name, reflected binary code, describes how the sequence is built: write the list out, mirror it, and prefix a 0 to the original half and a 1 to the reflection.
Type a decimal number like 13, or a binary string like 1101.
The 4 bit sequence
Counting up in Gray code. The highlighted bit is the one that changed from the row above: there is never more than one.
| n | Binary | Gray | Gray as decimal |
|---|---|---|---|
| 0 | 0000 | 0000 | 0 |
| 1 | 0001 | 0001 | 1 |
| 2 | 0010 | 0011 | 3 |
| 3 | 0011 | 0010 | 2 |
| 4 | 0100 | 0110 | 6 |
| 5 | 0101 | 0111 | 7 |
| 6 | 0110 | 0101 | 5 |
| 7 | 0111 | 0100 | 4 |
| 8 | 1000 | 1100 | 12 |
| 9 | 1001 | 1101 | 13 |
| 10 | 1010 | 1111 | 15 |
| 11 | 1011 | 1110 | 14 |
| 12 | 1100 | 1010 | 10 |
| 13 | 1101 | 1011 | 11 |
| 14 | 1110 | 1001 | 9 |
| 15 | 1111 | 1000 | 8 |
How the conversion works
Both directions are only XOR gates, which is why the hardware is trivial. Binary to Gray is a single layer of n − 1 XORs, one for every bit below the top, all working at once. Gray to binary reads as a chain, because each bit needs the one above it, but the chain is a running XOR and those fold into a tree: n bits need only log2 n layers, which is the same trick the shift-and-XOR version below uses.
Binary to Gray
gray = n ⊻ (n >> 1)
Copy the most significant bit unchanged, then make each following Gray bit the XOR of the binary bit in that position with the binary bit above it.
Gray to binary
bᵢ = gᵢ ⊻ bᵢ₊₁
Again the top bit is copied straight across. Each following binary bit is the XOR of the Gray bit there with the binary bit you have just worked out, so it has to run top down.
Build it yourself: an XOR per bit below the top converts binary to Gray in one layer of gates. Open the simulator and wire four toggles through three XORs to see it work.
Why it exists
The point of Gray code is that only one bit moves at a time. If you read an ordinary binary counter at the exact moment it steps from 0111 to 1000, the bits do not all flip at once in the real world, and you can sample a value that was never intended — anything from 0000 to 1111. With Gray code the worst case is that you catch the old value or the new one, because only one bit is in motion.
That is why rotary encoders, linear position sensors and anything crossing between two clock domains tends to use it. And it is why the edges of a Karnaugh map are labelled 00, 01, 11, 10 rather than in counting order: neighbouring squares then differ in exactly one variable, which is the whole reason grouping them cancels that variable out.
Questions
What is Gray code?
A way of ordering binary numbers so that consecutive values differ in exactly one bit. Counting 0, 1, 2, 3 in ordinary binary goes 00, 01, 10, 11, where the step from 01 to 10 changes both bits at once. In Gray code the same four values are 00, 01, 11, 10, and every step changes one bit only.
How do you convert binary to Gray code?
Exclusive-or the number with itself shifted right by one place: gray = n XOR (n >> 1). The top bit is copied unchanged, and each lower Gray bit is the XOR of the binary bit in that position with the one above it.
How do you convert Gray code back to binary?
Work down from the top. The most significant bit is copied unchanged, and each following binary bit is the XOR of the Gray bit in that position with the binary bit you just produced. Repeatedly XOR-shifting does the same thing in a few operations.
What is Gray code used for?
Anywhere a value is read while it might be changing. Rotary encoders and position sensors use it so a reading taken mid-transition is off by at most one step rather than wildly wrong. Karnaugh maps use it along their edges so that neighbouring squares differ in one variable, which is what makes grouping work.