On this page
Count from 3 to 4 in binary and something awkward happens: 011 becomes 100, and all three bits flip at once. A Gray code is a way of listing the same numbers so that this never happens. Every step changes exactly one bit, and the idea turns up in sensors, puzzles and graph theory.
What is a Gray code?
The reflected binary code is "an ordering of the binary numeral system such that two successive values differ in only one bit", in the words of the Wikipedia article on Gray code. It is named after Frank Gray of Bell Telephone Laboratories, whose patent US 2,632,058, "Pulse code communication", granted on 17 March 1953, designates it "the reflected binary code". If you can convert decimal to binary by hand, you have everything you need.
How do you build the binary reflected Gray code?
The name tells you the method: reflect, then prefix.
Start with the 1-bit list: 0, 1.
Write the list, then write it again in reverse order (the reflection): 0, 1, 1, 0.
Put 0 in front of the first half and 1 in front of the second half: 00, 01, 11, 10.
Repeat. Reflect 00, 01, 11, 10 to get 10, 11, 01, 00, prefix 0s and 1s, and you have the 3-bit code: 000, 001, 011, 010, 110, 111, 101, 100.
Why does it work? Inside each half, neighbours are old neighbours with the same prefix, so they still differ in one bit. At the join, reflection puts the same old string on both sides, so only the prefix changes. Even the last string, 100, differs from the first, 000, in one bit, so the list closes into a loop.
How do you convert binary to Gray code with XOR?
You do not have to build the whole list to find one entry. The nth Gray codeword is n XOR floor(n/2), the formula given in the OEIS entry A003188, "Decimal equivalent of Gray code for n". In code, floor(n/2) is a right shift, so g = n ^ (n >> 1).
The 3-bit worked table, as n: n in binary, n >> 1, then the XOR:
0: 000, 000, Gray 000
1: 001, 000, Gray 001
2: 010, 001, Gray 011
3: 011, 001, Gray 010
4: 100, 010, Gray 110
5: 101, 010, Gray 111
6: 110, 011, Gray 101
7: 111, 011, Gray 100
In decimal the Gray column is 0, 1, 3, 2, 6, 7, 5, 4, the first eight terms on the OEIS page and the same list reflection gave.
Going back is a running XOR from the left. For Gray 101: the first bit stays 1; the next binary bit is 1 XOR 0 = 1; the last is 1 XOR 1 = 0. So 101 decodes to 110, which is 6. If XOR still feels unfamiliar, it is the same operation behind the winning rule in the game of Nim.
Why do rotary encoders use Gray code?
An absolute rotary encoder reads a shaft's angle with one sensor per bit. In plain binary, 011 to 100 needs three sensors to switch at once. They never quite do, and as the Gray code article puts it, "a misread will result from some of the bits changing before others". For a moment the reader could see 111 or 000, a position nowhere near the true one. With a Gray code only one bit changes per step, so a slow sensor can only report the old position or the new one.
How is Gray code linked to the Tower of Hanoi and the cube?
Write out the bit that changes at each step of the 3-bit Gray code: bit 0, 1, 0, 2, 0, 1, 0. Now take the Tower of Hanoi with three discs. The Wikipedia article on the puzzle says the minimum number of moves is 2ⁿ − 1, which is 7 here, and that in the Gray-code solution "the bit changed each move corresponds to the disk to move", with the lowest bit as the smallest disc. So the moves are: small, middle, small, large, small, middle, small.
Now picture the eight 3-bit strings as the corners of a cube, with an edge wherever two strings differ in one bit. The Gray code walks along edges and visits every corner once: a Hamiltonian path, and with the step from 100 back to 000, a Hamiltonian cycle. The OEIS entry states this for every n of 2 or more: the list, closed back to its start, is "a Hamilton cycle" in the n-cube. Our explainer on Hamiltonian cycles, Dirac's test and the knight's tour looks at such tours in other graphs.
For binary, XOR and graphs, the Fermi Concept Library covers concepts across mathematics and computing, and the Fermi Problem Library has a step-by-step solver that explains the reasoning.



