Binary to Gray Code Converter
The most-significant Gray bit copies the binary MSB, while the other three Gray bits XOR adjacent binary inputs. The resulting reflected Gray-code sequence changes one bit between adjacent integer values.
This combinational circuit converts a 4-bit binary word into reflected Gray code, whose consecutive integer codewords differ in exactly one bit.
What You'll Learn
- Derive G3 = B3 and each lower Gray bit as an XOR of adjacent binary bits.
- Convert all 16 four-bit binary values to reflected Gray code.
- Verify that consecutive Gray codewords differ in exactly one position.
- Explain why Gray coding reduces boundary ambiguity in position encoders.
- Distinguish reduced simultaneous switching from synchronization or error correction.
How It Works
The most-significant Gray bit is copied directly: G3 = B3. Each remaining Gray bit is the XOR of two adjacent binary bits: G2 = B3 ⊕ B2, G1 = B2 ⊕ B1, and G0 = B1 ⊕ B0. The circuit therefore needs only three XOR gates plus the direct B3 connection.
Reflected Gray code orders the 16 codewords so that adjacent integer values have Hamming distance one. The binary transition from 0111₂ to 1000₂ changes all four bits, while the corresponding Gray transition is 0100₂ to 1100₂ and changes only G3. This property limits ambiguity when several physical binary signals cannot be guaranteed to change at precisely the same instant.
Gray coding does not make a sampled asynchronous bus automatically safe. In clock-domain crossings, designers still use synchronizers and constrain how the Gray-coded state advances. The code reduces the number of changing bits between adjacent states; it does not remove metastability or correct arbitrary sampling errors.
Truth Table
The table lists the complete 4-bit reflected Gray-code mapping. Inputs and outputs are ordered from most-significant bit to least-significant bit.
| Inputs | Output | |||||||
|---|---|---|---|---|---|---|---|---|
| B3 | B2 | B1 | B0 | G3 | G2 | G1 | G0 | |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Binary zero maps to Gray zero. |
| 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | The first increment changes only G0. |
| 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | |
| 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | |
| 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | |
| 0 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | |
| 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | |
| 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | Decimal 7 maps to 0100₂. |
| 1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | Across 7 → 8, Gray changes only G3 even though all binary bits change. |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | |
| 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | |
| 1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | |
| 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | |
| 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | |
| 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | |
| 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | Decimal 15 maps to 1000₂. |
Boolean Expression
The most-significant bit passes through unchanged.
G2 reports whether B3 and B2 differ.
G1 reports whether B2 and B1 differ.
G0 reports whether B1 and B0 differ.
Try It Step-by-Step
Set the listed controls in the embedded circuit, then compare the observed modeled state with the expected result.
- 1B3..B0=0000Expected:
G3..G0 = 0000What you'll see:All adjacent binary pairs are equal, so every Gray output is LOW. - 2B3..B0=0111Expected:
G3..G0 = 0100What you'll see:Binary 0111₂, decimal 7, produces Gray 0100₂. - 3B3..B0=1000Expected:
G3..G0 = 1100; only G3 changes across the 7-to-8 boundaryWhat you'll see:Binary 1000₂, decimal 8, produces Gray 1100₂. Comparing it with the previous step shows the one-bit transition directly. - 4B3..B0=1111Expected:
G3..G0 = 1000What you'll see:At binary 1111₂, adjacent bits are equal and only the copied most-significant Gray bit remains HIGH.
Components Used
Applications and Design Context
Rotary and linear encoders. Adjacent positions differ by one code bit, reducing ambiguous readings near a physical boundary.
Asynchronous FIFO pointers. Read and write pointers are commonly Gray-coded before synchronization so only one pointer bit changes per increment.
Position and angle sensing. Absolute encoders use Gray patterns to make neighboring mechanical positions easier to distinguish reliably.
Karnaugh maps. Row and column labels use Gray order so adjacent cells differ in one variable, preserving logical adjacency.
State assignment and low-transition interfaces. Some designs choose Gray-like state sequences to reduce simultaneous switching and simplify neighboring-state detection.
Frequently Asked Questions
Why does Gray code change only one bit between consecutive values?
Reflected Gray code is constructed so each adjacent codeword differs in one position. The binary-to-Gray XOR equations preserve that ordering for consecutive binary integers.
Is Gray code a weighted numeric representation?
No. Gray bits do not carry fixed powers-of-two weights. Convert Gray back to binary before ordinary arithmetic or magnitude interpretation.
How do I convert Gray code back to binary?
Set B3 = G3, then accumulate XOR downward: B2 = B3 ⊕ G2, B1 = B2 ⊕ G1, and B0 = B1 ⊕ G0.
Does Gray code eliminate metastability?
No. An asynchronous signal can still violate setup or hold time. Gray coding limits a legal adjacent transition to one changing bit, while synchronizer stages and timing constraints manage metastability risk.
What happens if the source skips several Gray states?
The endpoints may differ in multiple bits, so the single-bit-transition guarantee no longer applies to that observed jump. Systems such as asynchronous FIFOs constrain pointer movement and sampling assumptions accordingly.