How to build a binary to Gray code converter
A binary to Gray code converter keeps the top bit and XORs every pair of neighbouring bits: for 4 bits, G3 = B3, G2 = B3 XOR B2, G1 = B2 XOR B1 and G0 = B1 XOR B0. Three XOR gates do the whole conversion.
In Gray code consecutive numbers differ in exactly one bit, which is why it is used for rotary encoders and to avoid glitches when a multi-bit value changes. Converting from binary needs no carries at all: each Gray bit just records whether a binary bit differs from the one to its left. Going back the other way is a running XOR from the top (B3 = G3, B2 = B3 XOR G2, and so on), which the Gray code converter tool computes for any number. All 16 four-bit codes are checked before the page is built.
Open this circuit in the lab →
What you need
- 3× XOR: G2 = B3 XOR B2, G1 = B2 XOR B1, G0 = B1 XOR B0
Step by step
- Place the binary inputs
B3(most significant) toB0. - The top Gray bit is the top binary bit: wire B3 straight to
G3. - For each lower bit, XOR the binary bit with the one above it:
G2 = B3 XOR B2,G1 = B2 XOR B1,G0 = B1 XOR B0. - Probe
G3toG0and count the inputs up from 0000: every step changes exactly one Gray output, even 0111 to 1000, where all four binary bits flip.
4-bit binary to Gray code table
| Decimal | Binary B3..B0 | Gray G3..G0 |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Read down the Gray column: each row differs from the one above in a single bit, and 15 (1000) differs from 0 (0000) in one bit too, so the code wraps cleanly. In one line the conversion is G = B XOR (B shifted right by 1).
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 binary to Gray code converter in the lab →Frequently asked
How do you convert binary to Gray code?
Keep the most significant bit, then XOR each pair of adjacent bits moving right: G3 = B3, G2 = B3 XOR B2, G1 = B2 XOR B1, G0 = B1 XOR B0. Equivalently, Gray = binary XOR (binary shifted right by one).
How many XOR gates does a 4-bit binary to Gray converter need?
Three. The top bit passes straight through, and each of the other three Gray bits is one XOR of two neighbouring binary bits.
How do you convert Gray code back to binary?
Keep the top bit, then XOR each new Gray bit with the binary bit you just produced: B3 = G3, B2 = B3 XOR G2, B1 = B2 XOR G1, B0 = B1 XOR G0. Unlike the forward direction, each bit depends on the one before, so the XORs form a chain.
Why is Gray code used?
Because only one bit changes between consecutive values, a reading taken while the value is changing can be off by at most one step. That makes it the standard code for rotary and linear position encoders, and it is also how Karnaugh maps order their rows and columns.