Binary to Gray code and back: conversion table, K-maps and the XOR circuit
Gray code is a binary code in which consecutive numbers differ in exactly one bit. To convert binary to Gray, keep the first bit and XOR each pair of neighbouring bits; to convert back, XOR each Gray bit with the binary bit before it. 1011 in binary is 1110 in Gray code.
This guide explains why one-bit steps matter, gives the 3-bit and 4-bit conversion tables, derives each Gray bit with a Karnaugh map and checks it against its XOR form, converts Gray code back to binary, and draws both circuits. The tables are computed by the Tangibool engine, and each claim that two expressions are the same links to a free checker that tests them on all 16 rows.
What Gray code is, and why one bit at a time matters
Count from 0 to 15 in ordinary binary and some steps change several bits at once: 0111 to 1000 changes all four. In a real device the bits never change at exactly the same instant, so a reading taken mid-change can see a mixture, such as 1111 or 0000, a value far from either neighbour. Gray code orders the numbers so that each step changes exactly one bit. A reading taken mid-change can then only be the old value or the new one.
The code is named after Frank Gray, a Bell Labs researcher who patented its use in 1953 (the patent was filed in 1947). It is also called the reflected binary code, after the way it is built.
Where it is used:
- Rotary and linear encoders. A disc with tracks of light and dark segments reports the angle of a shaft. With a Gray-coded disc, a sensor sitting on a boundary is off by at most one position.
- Karnaugh maps. The rows and columns of a K-map are labelled 00, 01, 11, 10, the 2-bit Gray code, so that neighbouring cells differ in one variable; see Karnaugh map grouping.
- Crossing clock domains. Hardware FIFOs pass their read and write counters between circuits with different clocks in Gray code, so a counter caught mid-change is never wildly wrong.
- Digital radio. Modulation schemes label neighbouring signal points with Gray codes, so the most likely error, a point mistaken for its neighbour, corrupts only one bit.
Building the code by reflection
The n-bit Gray code is built from the (n − 1)-bit one in three steps:
- Write the (n − 1)-bit list.
- Below it, write the same list in reverse order, like a reflection.
- Put a 0 in front of every code in the top half and a 1 in front of every code in the bottom half.
From the 1-bit code 0, 1: reflect to get 0, 1, 1, 0, prefix to get 00, 01, 11, 10. Reflect that to get 00, 01, 11, 10, 10, 11, 01, 00, prefix to get the 3-bit code 000, 001, 011, 010, 110, 111, 101, 100. The reflection is why the step across the middle changes only the new first bit.
Binary to Gray code conversion
The rule, for a 4-bit number B3 B2 B1 B0:
G3 = B3, G2 = B3 ⊕ B2, G1 = B2 ⊕ B1, G0 = B1 ⊕ B0
The first Gray bit copies the first binary bit, and every other Gray bit is the exclusive or (XOR) of a binary bit and the one to its left. XOR is 1 when its inputs differ, so each Gray bit records whether the binary number changes there.
Example: 1011. G3 = 1. G2 = 1 ⊕ 0 = 1. G1 = 0 ⊕ 1 = 1. G0 = 1 ⊕ 1 = 0. So 1011 in binary is 1110 in Gray code.
3-bit Gray code table
In the table T stands for 1 and F for 0, and row n is the binary number n, with B2 as the most significant bit:
| # | B2 | B1 | B0 | G2 | G1 | G0 |
|---|---|---|---|---|---|---|
| 0 | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| 1 | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE |
| 2 | FALSE | TRUE | FALSE | FALSE | TRUE | TRUE |
| 3 | FALSE | TRUE | TRUE | FALSE | TRUE | FALSE |
| 4 | TRUE | FALSE | FALSE | TRUE | TRUE | FALSE |
| 5 | TRUE | FALSE | TRUE | TRUE | TRUE | TRUE |
| 6 | TRUE | TRUE | FALSE | TRUE | FALSE | TRUE |
| 7 | TRUE | TRUE | TRUE | TRUE | FALSE | FALSE |
Computed by the Tangibool engine
Read the G columns down the table: 000, 001, 011, 010, 110, 111, 101, 100, the reflected code built above. Each row differs from the next in one Gray bit, and so do the last row and the first.
4-bit Gray code table
| # | B3 | B2 | B1 | B0 | G3 | G2 | G1 | G0 |
|---|---|---|---|---|---|---|---|---|
| 0 | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| 1 | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | TRUE |
| 2 | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | TRUE | TRUE |
| 3 | FALSE | FALSE | TRUE | TRUE | FALSE | FALSE | TRUE | FALSE |
| 4 | FALSE | TRUE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE |
| 5 | FALSE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | TRUE |
| 6 | FALSE | TRUE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE |
| 7 | FALSE | TRUE | TRUE | TRUE | FALSE | TRUE | FALSE | FALSE |
| 8 | TRUE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | FALSE |
| 9 | TRUE | FALSE | FALSE | TRUE | TRUE | TRUE | FALSE | TRUE |
| 10 | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | TRUE | TRUE |
| 11 | TRUE | FALSE | TRUE | TRUE | TRUE | TRUE | TRUE | FALSE |
| 12 | TRUE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE |
| 13 | TRUE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE | TRUE |
| 14 | TRUE | TRUE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE |
| 15 | TRUE | TRUE | TRUE | TRUE | TRUE | FALSE | FALSE | FALSE |
Computed by the Tangibool engine
Row 7 (binary 0111) has the Gray code 0100 and row 8 (binary 1000) has 1100: the step that changes four binary bits changes one Gray bit. Row 11 is the example above, 1011 to 1110.
| Decimal | Binary | Gray | Decimal | Binary | Gray |
|---|---|---|---|---|---|
| 0 | 0000 | 0000 | 8 | 1000 | 1100 |
| 1 | 0001 | 0001 | 9 | 1001 | 1101 |
| 2 | 0010 | 0011 | 10 | 1010 | 1111 |
| 3 | 0011 | 0010 | 11 | 1011 | 1110 |
| 4 | 0100 | 0110 | 12 | 1100 | 1010 |
| 5 | 0101 | 0111 | 13 | 1101 | 1011 |
| 6 | 0110 | 0101 | 14 | 1110 | 1001 |
| 7 | 0111 | 0100 | 15 | 1111 | 1000 |
The compact table is the engine table above, rewritten with 1 and 0.
Deriving each Gray bit with a Karnaugh map
The XOR formulas can be read straight off the truth table with Karnaugh maps, one per output bit. The map for a 4-bit function has B3 B2 down the side and B1 B0 across the top, both in Gray-code order.
- G2 is 1 for the binary numbers 4 to 11: Σm(4, 5, 6, 7, 8, 9, 10, 11). On the map those are the whole row B3 B2 = 01 and the whole row 10, two groups of four. The Karnaugh map solver circles them as B3′·B2 and B3·B2′, and the sum B3′B2 + B3B2′ is exactly B3 ⊕ B2.
- G1 is 1 for Σm(2, 3, 4, 5, 10, 11, 12, 13), where B2 and B1 differ. The solver gives B2′·B1 + B2·B1′, which is B2 ⊕ B1.
- G0 is 1 for Σm(1, 2, 5, 6, 9, 10, 13, 14), where B1 and B0 differ. The solver gives B1′·B0 + B1·B0′, which is B1 ⊕ B0.
- G3 is B3, the bottom half of the map, with nothing to simplify.
Each map is two blocks of four, the pattern of a two-input XOR spread over a 4-variable map: the function depends on two of the four inputs, and is 1 where those two differ.
Gray code to binary conversion
Going back, each binary bit is the XOR of the Gray bits from the left up to that position:
B3 = G3, B2 = G3 ⊕ G2, B1 = G3 ⊕ G2 ⊕ G1, B0 = G3 ⊕ G2 ⊕ G1 ⊕ G0
The quick way to apply it by hand is a running XOR: copy the first bit, then for each next position, XOR the Gray bit with the binary bit you just wrote. B2 = B3 ⊕ G2, B1 = B2 ⊕ G1, B0 = B1 ⊕ G0.
Example: Gray 1110. B3 = 1. B2 = 1 ⊕ 1 = 0. B1 = 0 ⊕ 1 = 1. B0 = 1 ⊕ 0 = 1. So Gray 1110 is binary 1011, the number we started with.
Here is the whole conversion, with the Gray code as input. Row n has the Gray code whose bits read as the binary number n:
| # | G3 | G2 | G1 | G0 | B3 | B2 | B1 | B0 |
|---|---|---|---|---|---|---|---|---|
| 0 | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| 1 | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | TRUE |
| 2 | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | TRUE | TRUE |
| 3 | FALSE | FALSE | TRUE | TRUE | FALSE | FALSE | TRUE | FALSE |
| 4 | FALSE | TRUE | FALSE | FALSE | FALSE | TRUE | TRUE | TRUE |
| 5 | FALSE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE |
| 6 | FALSE | TRUE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE |
| 7 | FALSE | TRUE | TRUE | TRUE | FALSE | TRUE | FALSE | TRUE |
| 8 | TRUE | FALSE | FALSE | FALSE | TRUE | TRUE | TRUE | TRUE |
| 9 | TRUE | FALSE | FALSE | TRUE | TRUE | TRUE | TRUE | FALSE |
| 10 | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | FALSE |
| 11 | TRUE | FALSE | TRUE | TRUE | TRUE | TRUE | FALSE | TRUE |
| 12 | TRUE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE |
| 13 | TRUE | TRUE | FALSE | TRUE | TRUE | FALSE | FALSE | TRUE |
| 14 | TRUE | TRUE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE |
| 15 | TRUE | TRUE | TRUE | TRUE | TRUE | FALSE | TRUE | FALSE |
Computed by the Tangibool engine
Row 14 is Gray 1110, and its B columns read 1011. The last binary bit, B0, is the parity of all four Gray bits, so its Karnaugh map is a checkerboard that cannot be grouped at all; the Karnaugh map solver shows eight single cells.
Why the decoding works
Substitute the encoding into the decoding and the XORs cancel in pairs, because x ⊕ x = 0. For B2: G3 ⊕ G2 = B3 ⊕ (B3 ⊕ B2) = B2, and the equivalence checker confirms it. For B1, the round trip B3 ⊕ (B3 ⊕ B2) ⊕ (B2 ⊕ B1) comes back to B1, and for B0 the four-term chain comes back to B0. So converting to Gray code and back always returns the original number.
The circuits
Binary to Gray: three XOR gates for 4 bits, all working at once. G3 is a wire from B3; G2, G1 and G0 each come from one XOR gate fed by two neighbouring binary bits. An n-bit converter needs n − 1 XOR gates, and the result is ready after one gate delay.
Gray to binary: three XOR gates in a chain. B3 is a wire from G3; the first XOR combines G3 and G2 into B2, the second combines B2 with G1 into B1, and the third combines B1 with G0 into B0. Each gate waits for the one before, so an n-bit converter has a delay of n − 1 gates. Faster versions compute each B bit with its own tree of XOR gates.
To see a converter drawn, type one output into the truth table generator, for example G3 XOR G2 XOR G1, which draws the XOR gates for B1.
Worked example: an 8-position encoder disc
A 3-bit absolute encoder divides a disc into 8 sectors and reads three tracks, one per bit, with three sensors in a line. Suppose the shaft stops exactly on the boundary between sector 3 and sector 4, and each sensor reads whichever side it happens to see.
- With plain binary, sector 3 is 011 and sector 4 is 100. All three bits differ across the boundary, so each sensor can read either value, and the encoder can report any of the 8 codes, from 000 to 111: a shaft at position 3½ could be reported at 0 or at 7.
- With Gray code, sector 3 is 010 and sector 4 is 110. Only the first bit differs, so the encoder reports 010 or 110, which is 3 or 4. The error can never be more than one position.
The same holds at every boundary, including the one between sector 7 (Gray 100) and sector 0 (Gray 000), because the reflected code also changes one bit when it wraps around.
In code
For whole numbers, the conversion is one line: g = n ^ (n >> 1) in C, Java, JavaScript or Python, where ^ is bitwise XOR and >> shifts right. 11 becomes 14, which is binary 1110. Going back takes a short loop: start with b = 0, then repeat b ^= g and g >>= 1 until g is 0. For g = 14 the loop gives 11 again.
Tangibool's tools work with single true-or-false values, not whole numbers, and they reject the caret ^, because it means XOR in some languages and a power in others; write XOR there, bit by bit, as in the tables above.
Common mistakes
- Decoding with the encoding formula. Gray to binary is not “XOR neighbouring Gray bits”. Doing that for B1 gives G2 ⊕ G1 = (B3 ⊕ B2) ⊕ (B2 ⊕ B1) = B3 ⊕ B1, which is not B1: it is wrong whenever B3 is 1.
- XORing the wrong neighbour. Each Gray bit uses the binary bit itself and the one to its left, the more significant one. The leftmost bit is copied.
- Forgetting to copy the first bit. G3 = B3 and B3 = G3, in both directions.
- Reading Gray code as a binary number. Gray 1110 is not fourteen; it is eleven. Convert to binary first.
- Expecting Gray code arithmetic to work. Gray codes are for counting and reading positions, not for adding. Convert to binary, calculate, and convert back.
Try it yourself
- Type a Gray bit as a list of minterms, such as
Σm(B3, B2, B1, B0; 2, 3, 4, 5, 10, 11, 12, 13), into the Karnaugh map solver to see the two groups. - Compare the result with its XOR form,
B2 XOR B1, in the equivalence checker. - To build the whole conversion table, give the truth table generator one output at a time; its rows follow the binary order used above.
Frequently asked questions
What is Gray code?
A way of listing binary numbers so that each number differs from the next in exactly one bit. It is also called the reflected binary code.
How do you convert binary to Gray code?
Keep the leftmost bit, then XOR each bit with the bit to its left. 1011 becomes 1, 1 ⊕ 0, 0 ⊕ 1, 1 ⊕ 1, which is 1110.
How do you convert Gray code to binary?
Keep the leftmost bit, then XOR each Gray bit with the binary bit you have just written. 1110 becomes 1, 1 ⊕ 1, 0 ⊕ 1, 1 ⊕ 0, which is 1011.
What is the Gray code of 7?
7 is 0111 in binary and 0100 in 4-bit Gray code. 8 is 1000 in binary and 1100 in Gray code, so the step from 7 to 8 changes one bit.
Why is Gray code used?
Because only one bit changes between neighbouring values, a reading taken while the bits are changing is never far off. Encoders, Karnaugh maps, clock-domain crossings and digital radio use it for that reason.
Why is it called reflected binary code?
Because the n-bit code is built by writing the (n − 1)-bit code, then the same list reflected in reverse, and putting 0 before the first half and 1 before the second.
How many XOR gates does a binary to Gray converter need?
n − 1 for n bits: three for a 4-bit converter. The Gray to binary converter needs n − 1 as well, connected in a chain.
Is Gray code the same as BCD?
No. BCD writes each decimal digit in four binary bits; Gray code reorders binary numbers so that neighbours differ in one bit.
Related pages
- Guides: BCD to seven-segment decoder
- Glossary: Gray code and Karnaugh map
- Karnaugh map grouping and Lesson 11: Karnaugh maps
- XOR, XOR identities and three-input XOR is parity
- Practice: Karnaugh maps with 4 variables, with answers
Written by Muaz Notiar. The Tangibool engine (semantics tangibool-semantics/1, engine 1.0.0) computed the 3 truth tables marked “Computed by the Tangibool engine”, and the 11 tool results the guide links to were re-run through the tools' own code in the build of and match what the text says. © 2026 Foreranger LTD.