Skip to content

Digital Logic

What is Gray code, and why do encoders use it?

Gray code is a way of counting in binary where only one bit changes between each number and the next. Ordinary binary can change several bits at once: 3 to 4 is 011 to 100, all three bits. If those bits are read while they're changing, the reading can be anything. In Gray code a reading caught mid-step is always one of the two neighbours.

The count

DecimalBinaryGray
0000000
1001001
2010011
3011010
4100110
5101111
6110101
7111100

Read down the Gray column: each row differs from the one above in exactly one place, and 7 back to 0 is one bit too. It's also called reflected binary: the lower half of the list is the upper half in reverse with the top bit set.

Why encoders need it

A rotary encoder reports a shaft's angle with a disc printed in rings, one ring per bit, read by a row of sensors. No two sensors are ever perfectly in line. When the disc crosses from 3 to 4 in binary, all three bits change, and for a moment some heads see the new bit and some the old one: 000, 111, 101, any of eight values. A robot arm told it had jumped half a turn could lurch.

Print the disc in Gray code and only one ring changes at each boundary. Whichever head is early or late, the reading is the sector before or the sector after. Never anything wild.

Live experiment

Spin a code disc

A code disc of eight sectors turns under three read heads that sit slightly out of line, as real ones do. Printed in binary, a step like 3 to 4 changes every bit and the heads catch wild mixes. Print it in Gray code and every step changes one bit.

Try it yourself: free lesson

“Gray code” is lesson 5 of 6 in Nand Narrows, Digital Logic World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Binary to Gray and back

Each Gray bit is its binary bit XOR the binary bit above it. The top bit has nothing above, so it's copied:

Gi = Bi ⊕ Bi+1

For 5, binary 101: G2 = 1, G1 = 1 ⊕ 0 = 1, G0 = 0 ⊕ 1 = 1, giving 111. In code, it's a single line:

unsigned int gray = n ^ (n >> 1);

Going back, work down from the top: B2 = G2, then each binary bit is the binary bit above it XOR the Gray bit in its own place, B1 = B2 ⊕ G1 and B0 = B1 ⊕ G0. In hardware that's a chain of XORs, each passing its answer to the next.

Frank Gray of Bell Labs was granted a patent for it in 1953, for a vacuum tube that turned voltages into binary. Today it's in every absolute encoder on a robot joint or a machine tool.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Gray code” is lesson 5 of 6 in Nand Narrows, Digital Logic World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Or start from the very first lesson, no account needed

Work it out

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3Binary to Gray, G1 is…
    Show the answer

    Answer: B2 XOR B1. Each Gray bit is its binary bit XOR the one above. The top bit has nothing above, so G2 = B2.

    • Why not “B1 XOR B0”? B1 XOR B0 compares B1 with the bit below. Gray bits use the bit above, so G0 is the one that's B1 XOR B0.
    • Why not “B1”? G1 isn't just B1: each Gray bit compares its binary bit with the one above it.
    • Why not “B2”? Only the top Gray bit copies its binary bit, because it has nothing above it. That's G2 = B2.
  2. Question 2 of 3Binary 101 (5) in Gray code is…
    Show the answer

    Answer: 111. G2 = 1. G1 = 1 XOR 0 = 1. G0 = 0 XOR 1 = 1. So 111, which matches row 5 of the table.

    • Why not “100”? 100 is Gray code for 7, the last step before the count wraps round.
    • Why not “101”? 101 is the binary itself. Converting XORs each bit with the one above, which changes the middle and bottom bits here.
    • Why not “110”? 110 is Gray code for 4. For 5: G2 = 1, G1 = 1 XOR 0 = 1, G0 = 0 XOR 1 = 1.
  3. Question 3 of 3A disc reader catches binary mid-change from 011 to 100. It could read…
    Show the answer

    Answer: Almost anything: 000, 111, 101… any mix of old and new bits. Three bits change, and they never change at exactly the same instant. Each bit might be old or new, so any of eight values can appear for a moment. In Gray code only one bit changes, so the reading is always one of the two neighbours.

    • Why not “Only 011 or 100”? With three bits changing at slightly different instants, a mix of old and new bits can appear, not just the two clean values.
    • Why not “Always 100”? Nor does it jump cleanly to the new value: any head that's late still shows its old bit.
    • Why not “Always 011”? Each head flips at its own moment, so the reading isn't stuck at the old value until all three agree.
Keep practising: free lesson

“Gray code” is lesson 5 of 6 in Nand Narrows, Digital Logic World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Questions people ask

What is Gray code used for?

Anywhere a count is read while it's changing: rotary and linear position encoders, counters passed between two clocks inside chips, and the row and column order of Karnaugh maps. A reading caught mid-step is always one of the two neighbours, never a wild value.

How do you convert binary to Gray code?

Keep the top bit, then make each Gray bit the XOR of its binary bit and the binary bit above it. In code it's one line: gray = n XOR (n shifted right by one). Binary 101 becomes 111.

How do you convert Gray code back to binary?

Keep the top bit. Each lower binary bit is the binary bit above it XOR the Gray bit in its own place, working down from the top, so each step uses the bit just made. A chain of XORs does it in hardware.

Every topic