Conversion & CalculationLive🔒 Private

Gray Code Converter

Convert between binary and Gray code. Free online Gray code converter. No signup, 100% private, browser-based.

Gray Code Converter

Gray code

1111

Gray decimal

15

How it works

Gray code (reflected binary code) is a binary numbering where adjacent values differ by exactly one bit. For example: 0=000, 1=001, 2=011, 3=010, 4=110... Standard binary: 3=011, 4=100 — three bits change simultaneously, which causes transient errors in mechanical systems.

**Why Gray code matters** Rotary encoders (shaft position sensors) use Gray code to prevent misreading during transitions. In standard binary, when the reading changes from 0111 (7) to 1000 (8), all four bits change simultaneously. If the encoder is read at the exact transition moment, any combination of partially-changed bits could be misread as values 1–14. Gray code transitions only one bit at a time — the worst-case misread is ±1 position.

**Binary to Gray conversion** G_n = B_n (MSB unchanged). G_i = B_{i+1} XOR B_i for all other bits. To convert 1011 (binary 11): G₃=1, G₂=1 XOR 0=1, G₁=0 XOR 1=1, G₀=1 XOR 1=0 → Gray code = 1110.

**Gray to binary conversion** B_n = G_n (MSB unchanged). B_i = B_{i+1} XOR G_i. This cascaded XOR chain propagates from MSB to LSB. For hardware implementation, this is a simple combinational circuit.

**Karnaugh maps and Gray code** K-maps (Karnaugh maps) for logic minimization arrange inputs in Gray code order precisely to ensure adjacent cells differ by one variable. This visual representation makes identifying logic simplifications (grouping minterms) intuitive.

Frequently Asked Questions

How do rotary encoders use Gray code to measure position?
A rotary encoder has N tracks, each with alternating transparent/opaque segments, read by N optical sensors. In standard binary encoding, multiple tracks change simultaneously at certain positions — if sensors read slightly different times, momentary wrong readings occur. In Gray code encoding, only one track changes at each position transition. Even if one sensor reads early or late, the maximum error is ±1 position (one step). This makes Gray code ideal for mechanical encoders where timing alignment of multiple sensors is impractical. Absolute encoders with 12–16 tracks achieve 0.088°–0.005° angular resolution.
How is Gray code used in digital communications?
QAM (Quadrature Amplitude Modulation) in Wi-Fi, 5G, and cable modems uses Gray coding to map bit patterns to constellation points. Adjacent constellation points (differing by one symbol in position) differ by only one bit in Gray code. Since noise is most likely to cause a symbol to be misread as an adjacent symbol, Gray coding minimizes bit errors even when symbol errors occur — a symbol error causes only 1 bit error instead of potentially multiple. 256-QAM (used in Wi-Fi 5/6) transmits 8 bits per symbol with Gray-coded bit assignment to minimize BER at a given SNR.
How do you convert between Gray code and binary efficiently in software?
Binary to Gray (fast): gray = binary XOR (binary >> 1). In C: uint32_t gray = b ^ (b >> 1). Gray to binary (sequential, cannot be parallelized): b[n-1] = g[n-1] (MSB); b[i] = b[i+1] XOR g[i] for i from n-2 down to 0. In C: uint32_t b = g; b ^= (b >> 1); b ^= (b >> 2); b ^= (b >> 4); ... (cascade). These operations are O(1) in hardware (parallel prefix XOR) and O(log N) in software. The hardware parallel approach is: b = g; b ^= b>>16; b ^= b>>8; b ^= b>>4; b ^= b>>2; b ^= b>>1; for 32-bit values.
Why is Gray code called 'reflected binary' code?
The name comes from its construction method: the 1-bit Gray code is {0,1}. To create (N+1)-bit Gray code: write the N-bit code, then reflect it (write in reverse order), prefix original with 0 and reflection with 1. This reflection property means the N-bit Gray code is a Hamiltonian path on a hypercube — visiting all 2^N vertices while changing only one bit at a time. Frank Gray of Bell Labs patented it in 1953 for use in pulse code modulation (PCM) to reduce errors in telegraph transmission when mechanical encoders moved slightly.