Fig. 1 — Binary to Gray Code ConverterInteractive simulation

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.

InputsOutput
B3B2B1B0G3G2G1G0
00000000Binary zero maps to Gray zero.
00010001The first increment changes only G0.
00100011
00110010
01000110
01010111
01100101
01110100Decimal 7 maps to 0100₂.
10001100Across 7 → 8, Gray changes only G3 even though all binary bits change.
10011101
10101111
10111110
11001010
11011011
11101001
11111000Decimal 15 maps to 1000₂.

Boolean Expression

G3=B3G_3 = B_3

The most-significant bit passes through unchanged.

G2=B3⊕B2G_2 = B_3 \oplus B_2

G2 reports whether B3 and B2 differ.

G1=B2⊕B1G_1 = B_2 \oplus B_1

G1 reports whether B2 and B1 differ.

G0=B1⊕B0G_0 = B_1 \oplus B_0

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.

  1. 1
    B3..B0=0000
    Expected:G3..G0 = 0000
    What you'll see:All adjacent binary pairs are equal, so every Gray output is LOW.
  2. 2
    B3..B0=0111
    Expected:G3..G0 = 0100
    What you'll see:Binary 0111₂, decimal 7, produces Gray 0100₂.
  3. 3
    B3..B0=1000
    Expected:G3..G0 = 1100; only G3 changes across the 7-to-8 boundary
    What you'll see:Binary 1000₂, decimal 8, produces Gray 1100₂. Comparing it with the previous step shows the one-bit transition directly.
  4. 4
    B3..B0=1111
    Expected:G3..G0 = 1000
    What 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.

Continue Learning