← All Tools

Gray Code Converter

Convert between plain binary and reflected Gray code. Gray code (a.k.a. reflected binary code) ensures that consecutive values differ by exactly one bit — the basis for rotary encoders, Karnaugh maps, and glitch-free counters.

Gray sequence — n-1, n, n+1, n+2

Adjacent rows differ in exactly one bit (highlighted). This single-bit transition property is why Gray code is used in rotary encoders: a mis-aligned read still gives a neighbouring value rather than a wild jump.

About reflected Gray code

Binary → Gray: g = b ^ (b >> 1)
Gray → Binary: XOR every bit with all higher bits: b = g; b ^= b >> 1; b ^= b >> 2; ...
Named after Frank Gray (Bell Labs, 1953), reflected binary code is used in rotary and linear position encoders, Karnaugh maps, digital-to-analog converters, genetic algorithms, and Gray-scale modulation constellations in QAM.

Copied!