How to build a Gray to binary converter (4-bit)
A Gray to binary converter turns a 4-bit Gray code G3 G2 G1 G0 back into ordinary binary B3 B2 B1 B0 with three XOR gates: the top bit is copied, B3 = G3, and every lower bit is the XOR of the binary bit above it and its own Gray bit.
The binary to Gray converter XORs neighbouring binary bits, so its three XORs work side by side. Going back is a chain: B2 = G3 XOR G2, then B1 = B2 XOR G1 needs B2, and B0 = B1 XOR G0 needs B1, so the XORs ripple from the top bit down. Unrolled, each binary bit is the XOR of every Gray bit from its own position upward; B0 = G3 XOR G2 XOR G1 XOR G0 is the parity of the whole code. Gray code changes exactly one bit per step, which is why position sensors such as rotary encoders output it and why K-map rows and columns are labelled in Gray order; you convert back to binary as soon as you need to do arithmetic. All 16 codes are checked before the page is built. Convert numbers by hand with the Gray code converter.
Open this circuit in the lab →
What you need
- 3× XOR: B2 = G3 XOR G2, B1 = B2 XOR G1, B0 = B1 XOR G0 (B3 = G3 needs no gate)
Step by step
- Place inputs
G3toG0and probesB3toB0. - Wire G3 straight to B3.
- B2 = G3 XOR G2.
- B1 = B2 XOR G1, using the B2 you just made, then B0 = B1 XOR G0.
- Step through the Gray sequence 0000, 0001, 0011, 0010, 0110: the binary outputs count 0, 1, 2, 3, 4.
4-bit Gray to binary table
| Gray G3 G2 G1 G0 | Binary B3 B2 B1 B0 | Decimal |
|---|---|---|
| 0000 | 0000 | 0 |
| 0001 | 0001 | 1 |
| 0011 | 0010 | 2 |
| 0010 | 0011 | 3 |
| 0110 | 0100 | 4 |
| 0111 | 0101 | 5 |
| 0101 | 0110 | 6 |
| 0100 | 0111 | 7 |
| 1100 | 1000 | 8 |
| 1101 | 1001 | 9 |
| 1111 | 1010 | 10 |
| 1110 | 1011 | 11 |
| 1010 | 1100 | 12 |
| 1011 | 1101 | 13 |
| 1001 | 1110 | 14 |
| 1000 | 1111 | 15 |
Reading down the table, each Gray code differs from the one before it in exactly one bit, while the binary column can change several bits at once: 0111 to 1000 changes all four.
You can also read the full theory in the lesson on Gray code. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.
Build more parts from gates
Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.
Open the Gray to binary converter in the lab →Frequently asked
How do you convert Gray code to binary?
Copy the most significant bit, then XOR each binary bit you have just found with the next Gray bit: B3 = G3, B2 = B3 XOR G2, B1 = B2 XOR G1, B0 = B1 XOR G0.
How many XOR gates does a 4-bit Gray to binary converter need?
Three. An n-bit converter needs n - 1, the same as the binary to Gray direction, but chained one after another instead of side by side.
What is Gray code 1011 in binary?
B3 = 1, B2 = 1 XOR 0 = 1, B1 = 1 XOR 1 = 0, B0 = 0 XOR 1 = 1, so Gray 1011 is binary 1101, which is 13.
Why is Gray code used?
Consecutive values differ in only one bit, so a sensor reading a position, such as a rotary encoder, never sees a wildly wrong value while its bits change. K-map rows and columns use Gray order for the same reason: neighbouring cells differ in one variable.