Gray Code Converter & Visualizer
Interactive multi-bit Binary ↔ Gray Code converter, live bitwise XOR transformation solver, Clock Domain Crossing (CDC) multi-bit skew race simulator, and synthesizable Verilog RTL generator for asynchronous FIFOs.
Interactive Converter
4 Bits · 16 StatesVLSI Presets & Corner Cases
Design PatternsLoad classic semiconductor corner cases to inspect multi-bit toggling hazards and single-bit Gray transitions.
assign gray = bin ^ (bin >> 1);
// Gray to Binary (Bitwise Loop):
for (i = 0; i < WIDTH; i = i + 1)
bin[i] = ^(gray >> i);
Clock Domain Crossing (CDC) Multi-Bit Race & Glitch Simulator
Simulate what happens when asynchronous clock domains sample a multi-bit counter during a transition with non-zero physical routing skews. See why binary counters cause catastrophic FIFO pointer corruption, while Gray code guarantees single-bit safety.
Because 4 bits toggled simultaneously across physical routing wires with different RC delays, the destination clock sampled a false transient value (0010 = 2). This corrupts FIFO read/write pointers!
Because only ONE bit toggled (Bit 3), there is zero multi-bit wire skew. The destination clock samples either the old state (7) or the new state (8) — NEVER a phantom state!
Full Truth Table & Hamming Distance Table
Exhaustive enumeration for the current bit width. Click any row to load into the simulator.
| Dec | Hex | Binary Representation | Reflected Gray Code | Bit Changed | Hamming Dist | Action |
|---|
Synthesizable Verilog & SystemVerilog RTL Generator
Production-grade, parameterizable RTL templates for Binary-to-Gray converters, Gray-to-Binary decoders, and Asynchronous FIFO Gray counters.
// Verilog source will render here
Engineering Theory: Gray Code in Modern VLSI & ASIC Design
In-depth physical and architectural principles behind single-bit transitions, CDC synchronization, and FIFO pointers.
1. Frank Gray's Reflected Binary Theorem
Introduced by Bell Labs researcher Frank Gray in his 1953 patent, a reflected binary code organizes $N$-bit numbers such that every consecutive codeword differs in exactly one bit position ($\text{Hamming distance} \equiv 1$). The sequence is formed recursively by taking the $(N-1)$-bit sequence, reflecting it backwards, and prefixing '0' to the first half and '1' to the second half.
2. The Multi-Bit CDC Synchronization Hazard
When multi-bit buses cross asynchronous clock boundaries, standard 2-flip-flop synchronizers cannot be used directly on binary signals. Due to physical place-and-route delays, wire length skews, and clock arrival differences, each bit transitions at a slightly different picosecond. If the capture clock edge lands during this window, the synchronizer latches an illegal transient state. Gray coding guarantees that only a single bit toggles, eliminating intermediate states entirely.
3. FIFO Pointer Wrap-Around & Full/Empty Logic
In an async FIFO with depth $2^N$, pointers use an $(N+1)$-bit Gray code. The extra MSB disambiguates whether write and read pointers are in the same or opposite FIFO buffer laps:
• FIFO Empty: wptr_gray == rptr_gray (identical pointers).
• FIFO Full: wptr[N] != rptr[N] && wptr[N-1] != rptr[N-1] && wptr[N-2:0] == rptr[N-2:0] (the 2 MSBs invert while lower bits match).
4. Non-Power-of-2 FIFO Depths
Standard Gray code sequences naturally loop with Hamming distance 1 only when total depth is an exact power of 2 ($2^N$). If a non-power-of-2 depth (e.g., 24 entries) is needed, designers must symmetrically truncate the Gray code sequence around the center reflection axis to preserve the single-bit transition property when the pointer wraps from max back to 0.
5. Hardware Cost: Binary vs Gray
Binary-to-Gray conversion costs only $(N-1)$ parallel XOR gates with a logic depth of 1 gate ($\mathcal{O}(1)$ latency). Conversely, Gray-to-Binary conversion requires a cascaded XOR chain with a critical path delay of $(N-1)$ XOR gates ($\mathcal{O}(N)$ latency) or a parallel-prefix tree ($\mathcal{O}(\log N)$ latency). In high-speed ASIC flows, Gray counters increment in binary internally and register the Gray code output to prevent combinational glitches.
6. Rotary Optical Encoders
Beyond silicon CDC, Gray code tracks on mechanical and optical rotary encoder discs prevent sensor reading glitches. With physical contact imperfections, mechanical vibrations cannot produce wild jump errors because physical movement only ever toggles one track sensor at a time.
Frequently Asked Questions (VLSI Interview Prep)
B[N-1] = G[N-1]. Every subsequent binary bit is obtained by XORing the previously computed binary bit with the current Gray bit: B[i] = B[i+1] ^ G[i]. Alternatively, each binary bit B[i] is the odd parity (reduction XOR) of all Gray bits from index i up to N-1: B[i] = ^(G >> i).
bin_next = bin + 1), compute gray_next = bin_next ^ (bin_next >> 1), and register both values into flip-flops on the clock edge. This guarantees glitch-free registered outputs before sending across the CDC boundary.