In digital electronics, standard binary counting works well for arithmetic. However, when binary values change, multiple bits often switch at the exact same time.
For example, when transitioning from 0111 (7) to 1000 (8), all four bits invert simultaneously. In physical hardware, small differences in wire delays and gate switching speeds can cause intermediate spurious values (glitches), such as 1111 or 0000, for a few nanoseconds.
Gray code solves this problem. It is an unweighted unit-distance code where two consecutive values differ by only a single bit position.
A 4-bit binary to Gray code converter is a combinational circuit that translates standard 4-bit binary into reflected Gray code. In this tutorial, we will construct the complete 16-state truth table, derive the four output expressions using four-variable Karnaugh maps, and construct the minimal logic circuit.
1. What Is Gray Code?
Gray code, also known as Reflected Binary Code (RBC), is an ordering of the binary numeral system such that two successive values differ in only one bit position.
Consider counting from 0 to 3:
- Standard binary: 00 → 01 → 10 → 11 (moving from 01 to 10 flips two bits at once).
- Gray code: 00 → 01 → 11 → 10 (each transition changes exactly one bit).
Because only one bit changes per count step, Gray code is widely used in optical shaft encoders, digital communication systems, rotary angle sensors, and asynchronous FIFO memory buffers where clock-domain crossings must prevent multi-bit race conditions.
2. Understanding a 4-Bit Binary to Gray Code Converter
A 4-bit converter takes a four-bit binary input and generates four Gray code bits:
- Inputs: A (MSB, weight 8), B (weight 4), C (weight 2), D (LSB, weight 1).
- Outputs: G3 (MSB), G2, G1, G0 (LSB).
The conversion follows a straightforward mathematical rule:
- The most significant Gray bit is always equal to the most significant binary bit: G3 = A.
- Each subsequent Gray bit is obtained by XORing the current binary bit with the preceding binary bit:
- G2 = A ⊕ B
- G1 = B ⊕ C
- G0 = C ⊕ D
Let's prove this rule directly using Karnaugh maps starting from first principles.
3. Complete 4-Bit Binary to Gray Code Truth Table
Four binary inputs produce:
2⁴ = 16 possible combinations.
The complete conversion table is:
| Decimal | A | B | C | D | G3 | G2 | G1 | G0 |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
| 2 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |
| 3 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
| 4 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
| 5 | 0 | 1 | 0 | 1 | 0 | 1 | 1 | 1 |
| 6 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 |
| 7 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 |
| 8 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
| 9 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 |
| 10 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 |
| 11 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |
| 12 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 |
| 13 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
| 14 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
| 15 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Figure 1: Complete 16-row conversion truth table comparing 4-bit binary inputs with corresponding reflected Gray code outputs.
The truth table shows that all four outputs have their own Boolean functions.
We will simplify each output separately.
4. Deriving the Boolean Functions
From the truth table, identify the minterms where each Gray output is 1.
Using the variable order A, B, C, D:
G3(A,B,C,D) = Σm(8,9,10,11,12,13,14,15)
G2(A,B,C,D) = Σm(4,5,6,7,8,9,10,11)
G1(A,B,C,D) = Σm(2,3,4,5,10,11,12,13)
G0(A,B,C,D) = Σm(1,2,5,6,9,10,13,14)
All sixteen input combinations are valid, so there are no don't-care conditions in this conversion problem.
5. K-Map Arrangement
We need four separate four-variable Karnaugh maps.
Use the following arrangement for all four:
Rows: AB = 00, 01, 11, 10.
Columns: CD = 00, 01, 11, 10.
Both axes follow Gray-code order.
The exact values are provided below.
5.1 K-Map for G3
G3 = Σm(8,9,10,11,12,13,14,15)
| AB \ CD | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 00 | 0 | 0 | 0 | 0 |
| 01 | 0 | 0 | 0 | 0 |
| 11 | 1 | 1 | 1 | 1 |
| 10 | 1 | 1 | 1 | 1 |
The bottom two rows contain eight 1s.
These rows correspond to A = 1.
The entire group of eight cells eliminates B, C and D.
Therefore:
G3 = A
Figure 2: Four-variable K-Map for output G3 showing an 8-cell octet yielding the direct relation G3 = A.
5.2 K-Map for G2
G2 = Σm(4,5,6,7,8,9,10,11)
| AB \ CD | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 00 | 0 | 0 | 0 | 0 |
| 01 | 1 | 1 | 1 | 1 |
| 11 | 0 | 0 | 0 | 0 |
| 10 | 1 | 1 | 1 | 1 |
There are two separate groups of four.
The first group is the row AB = 01.
Here:
A = 0 and B = 1.
Therefore, the product term is:
A'B
The second group is the row AB = 10.
Here:
A = 1 and B = 0.
Therefore, the product term is:
AB'
Combining both groups:
G2 = A'B + AB'
This is the XOR function:
G2 = A XOR B
Figure 3: Four-variable K-Map for output G2 showing two 4-cell quads yielding G2 = A'B + AB' = A ⊕ B.
5.3 K-Map for G1
G1 = Σm(2,3,4,5,10,11,12,13)
| AB \ CD | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 00 | 0 | 0 | 1 | 1 |
| 01 | 1 | 1 | 0 | 0 |
| 11 | 1 | 1 | 0 | 0 |
| 10 | 0 | 0 | 1 | 1 |
Two groups of four can be formed.
The first group uses:
- Rows AB = 00 and 10.
- Columns CD = 11 and 10.
The first and last rows are adjacent through wrap-around.
In this group:
B = 0 and C = 1.
Therefore, the term is:
B'C
The second group uses:
- Rows AB = 01 and 11.
- Columns CD = 00 and 01.
In this group:
B = 1 and C = 0.
Therefore, the term is:
BC'
Combining both:
G1 = B'C + BC'
This is equivalent to:
G1 = B XOR C
Figure 4: Four-variable K-Map for output G1 with vertical wrap-around quad B'C and central quad BC' yielding G1 = B ⊕ C.
5.4 K-Map for G0
G0 = Σm(1,2,5,6,9,10,13,14)
| AB \ CD | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 00 | 0 | 1 | 0 | 1 |
| 01 | 0 | 1 | 0 | 1 |
| 11 | 0 | 1 | 0 | 1 |
| 10 | 0 | 1 | 0 | 1 |
There are two groups of four.
The first group is the entire CD = 01 column.
Here:
C = 0 and D = 1.
Therefore, the product term is:
C'D
The second group is the entire CD = 10 column.
Here:
C = 1 and D = 0.
Therefore, the product term is:
CD'
Combining both:
G0 = C'D + CD'
This is equivalent to:
G0 = C XOR D
Figure 5: Four-variable K-Map for output G0 showing column quads C'D and CD' yielding G0 = C ⊕ D.
6. Final Simplified Boolean Expressions
After simplifying all four Karnaugh maps, the results are:
G3 = A
G2 = A'B + AB' = A XOR B
G1 = B'C + BC' = B XOR C
G0 = C'D + CD' = C XOR D
The most significant output bit is directly connected to the most significant input bit.
The remaining three outputs are generated using XOR operations on adjacent binary input bits.
These equations provide the complete logic description of the four-bit binary-to-Gray converter.
7. Constructing the Logic Circuit
The circuit can be implemented using three two-input XOR gates and one direct connection.
The connections are:
- A → G3 directly.
- A and B → XOR gate 1 → G2.
- B and C → XOR gate 2 → G1.
- C and D → XOR gate 3 → G0.
Figure 6: Complete 4-bit binary to Gray code converter logic circuit with 3 parallel XOR gates and direct MSB pass-through.
Diagram requirements:
- Four input lines A, B, C and D.
- Three XOR gates.
- One direct connection from A to G3.
- Correct adjacent-bit XOR connections.
- Four clearly labelled output lines G3, G2, G1 and G0.
- Do not connect XOR gates in a cascade.
- All three XOR gates must operate directly from the original binary input signals.
The circuit is combinational, so the four Gray outputs depend only on the current four binary inputs.
8. Worked Examples
Let's test the simplified Boolean equations with two sample binary inputs.
Example 1: Convert Binary 1011 into Gray Code
Input bits:
A = 1, B = 0, C = 1, D = 1
Evaluate each output:
G3: G3 = A = 1
G2: G2 = A XOR B = 1 XOR 0 = 1
G1: G1 = B XOR C = 0 XOR 1 = 1
G0: G0 = C XOR D = 1 XOR 1 = 0
Result:
Binary 1011₂ = Gray 1110_Gray
Checking the truth table row for decimal 11 (binary 1011):
- G3 = 1
- G2 = 1
- G1 = 1
- G0 = 0
The derived equations match the truth table exactly.
Example 2: Convert Binary 1101 into Gray Code
Input bits:
A = 1, B = 1, C = 0, D = 1
Evaluate each output:
G3: G3 = A = 1
G2: G2 = A XOR B = 1 XOR 1 = 0
G1: G1 = B XOR C = 1 XOR 0 = 1
G0: G0 = C XOR D = 0 XOR 1 = 1
Result:
Binary 1101₂ = Gray 1011_Gray
Checking the truth table row for decimal 13 (binary 1101):
- G3 = 1
- G2 = 0
- G1 = 1
- G0 = 1
Both manual XOR evaluation and truth-table verification give identical results.
9. Why Is Gray Code Useful?
Gray code is widely preferred over natural binary in real-world engineering because of the single-bit transition property:
- Shaft and Rotary Encoders: Mechanical or optical encoders track rotational angle using concentric code tracks. If binary were used, reading a transition such as 011 to 100 on slightly misaligned photodetectors could momentarily register 000, 111, or 101, leading to major measurement error. With Gray code, any slight misalignment can only produce either the previous angle or the next angle—never a wild glitch.
- Asynchronous FIFOs: When passing pointers between independent clock domains, Gray-coded counters guarantee that only one bit toggles per clock cycle. Multi-bit metastability is prevented because the synchronizer flip-flops only need to resolve at most one changing bit.
- Karnaugh Map Labeling: The axis coordinates of Karnaugh maps themselves (00, 01, 11, 10) are sequenced in Gray code to ensure that adjacent physical cells differ by exactly one variable.
10. Common Mistakes
When converting between binary and Gray code, avoid these frequent pitfalls:
- Cascading XOR Gates: In binary-to-Gray conversion, each XOR gate takes two primary binary inputs (e.g. A and B, B and C). Do not confuse this with Gray-to-binary conversion, which cascades the previous Gray/binary output into the next stage.
- Inverting the MSB: The most significant bit G3 is identical to binary bit A; it never passes through an inverter or XOR gate.
- Treating Unused States as Don't-Cares: In 4-bit binary, all 16 states from 0000 to 1111 are legitimate numbers. None should be marked as don't-cares (unlike the BCD decoder in our BCD to 7-Segment Decoder Guide).
11. Frequently Asked Questions
What is a 4-bit binary-to-Gray code converter?
It is a combinational circuit that converts a four-bit binary input into its corresponding four-bit reflected binary Gray code.
What is the Boolean expression for binary-to-Gray conversion?
For a four-bit input ABCD:
G3 = A
G2 = A XOR B
G1 = B XOR C
G0 = C XOR D
How many XOR gates are needed?
Three two-input XOR gates are required. The most significant Gray bit is directly connected to the binary MSB.
Why does the MSB remain unchanged?
The most significant Gray bit is defined to be identical to the most significant binary bit. This also follows from the conversion rule.
Does a binary-to-Gray converter need don't-care conditions?
No. All sixteen combinations of a four-bit binary input are valid, so no input combinations need to be treated as don't-cares.
What is the difference between binary and Gray code?
Binary represents numbers using positional powers of two. Gray code is arranged so that consecutive values differ by exactly one bit.
Can a Gray code be converted back into binary?
Yes. The reverse conversion uses a running XOR operation beginning with the most significant bit.
12. Try the Existing Gray Code Converter
You can practise converting different binary inputs using the existing Gray Code Converter on this website.
Try a few values manually, then compare your answers with the converter.
You can also use the Karnaugh Map Solver to explore the four output functions independently.
Conclusion
A four-bit binary-to-Gray converter is a useful example of how a truth table can be simplified using Karnaugh maps to produce a compact logic circuit.
The four output functions simplify to one direct connection and three XOR expressions.
The final circuit needs only three XOR gates, making the implementation straightforward.
More importantly, deriving these expressions from the four Karnaugh maps helps explain why adjacent binary input bits are used to generate each lower Gray output.

