Skip to content

Gray Code

A Gray code orders distinct fixed-width binary words so every required neighboring pair differs in exactly one bit.

Core Idea

A Gray code orders distinct fixed-width binary words so each required neighboring pair differs in exactly one bit. A complete \(n\)-bit code lists all \(2^n\) words once; a cyclic one also makes the last and first words differ in one bit. Frank Gray's reflected binary construction—reverse a shorter list and prefix the two halves with 0 and 1—is one cyclic example, not every possible Gray code. The result controls local label changes, not all errors or sensor faults.[1][2][3]

How would you explain it like I'm…

One-Switch-at-a-Time Code

Imagine a row of light switches. Gray Code is a special order for going through all the on-off patterns where each step flips only one switch. You never have to flip two at once. So nobody gets confused halfway between steps.

The One-Bit-Change List

Computers write numbers with 0s and 1s called bits. A Gray code is a list of all the bit patterns of a certain length, put in an order where each pattern differs from the next by exactly one bit. For three bits, one such list is 000, 001, 011, 010, 110, 111, 101, 100. This helps machines like turning dials: when the dial is right between two spots, the reading is either the old one or the new one, not some random mix. It doesn't fix every mistake, though—it just keeps each small step clean.

Single-Bit-Step Binary Ordering

A Gray code orders binary words of a fixed length so that every pair of neighbors differs in exactly one bit. A complete n-bit Gray code uses all 2^n words once; a cyclic one also has the last word differing from the first by one bit. The best-known version, the reflected binary code, is built by taking a shorter list, putting 0 in front of it, then putting 1 in front of its reversed copy—the reversal keeps the seam in the middle to a one-bit change. It's useful for sensors like rotary encoders: during a step between adjacent positions only one signal is uncertain, so the reading is either the old or new position. Unlike an error-correcting code, though, it adds no redundancy, so noise, faulty sensors, or jumps across several positions can still produce wrong readings.

 

A Gray code is an ordering of distinct n-bit binary words in which each consecutive pair has Hamming distance one. A complete code lists all 2^n words; a cyclic code additionally requires the last and first words to be adjacent. Graph-theoretically, a complete listing is a Hamiltonian path on the n-dimensional hypercube, and a cyclic one is a Hamiltonian cycle. The reflected binary code is one construction among many: prefix the (n−1)-bit list with 0 and its reversal with 1; reversal makes the central boundary differ only in the new leading bit, while the inherited boundaries stay one-bit. For n = 3 it gives 000, 001, 011, 010, 110, 111, 101, 100, which is also cyclic. Frank Gray's patent used this reflection for pulse-code communication and acknowledged other forms with the same property. The guarantee is local: when transitioning between adjacent states with only the one differing signal ambiguous, a reader gets either the old or new valid word, not a spurious multi-bit mix. It provides no distance between valid codewords, so it is not error-correcting and can't protect against noise, faulty sensors, or nonadjacent jumps.

Tensions in Practice

Scope of Application

In a rotary absolute encoder, neighboring positions can be Gray-labeled so only one sensing track changes at a designed boundary. Analog Devices documents a concrete encoder position field read in Gray form and converted to binary. In a square 16-QAM constellation, MathWorks combines Gray orders along two axes so horizontal and vertical nearest points differ in one bit; that is a two-dimensional label map, not one linear sequence through all constellation points. Other constellation shapes need not achieve the same guarantee.[4][5][6]

Clarity

The decisive test is Hamming distance one on each declared adjacency edge. Natural binary counting fails at \(0111\to1000\), which flips four bits. The reflected three-bit list $000,001,011,010,110,111,101,100$ has unique full coverage, one-bit successive transitions and one-bit wraparound. A complete binary Gray path need not be cyclic; the reflected example happens to be. Its familiar formula \(g=b\oplus(b\mathbin{\mathrm{>>}}1)\) does not define every Gray ordering.[2][3]

Manages Complexity

The code replaces a lengthy case-by-case transition analysis with a local invariant: unique labels, complete state coverage where claimed, and one-bit differences on required edges. Reflection builds a full ordering recursively. Applied benefit still depends on the failure model. An ambiguous single encoder boundary is constrained differently from a distant symbol error or a broken sensing track; no redundancy has been added to correct arbitrary corruption.[1][7][5]

Abstract Reasoning

Treat \(n\)-bit words as vertices of a hypercube, connected when they differ in one bit. A full Gray code is a vertex-once path; cyclic closure is an extra final-to-first edge. For an application, declare its actual state-adjacency graph and test every edge relevant to the claimed benefit. A linear path's property cannot automatically be exported to all neighbor pairs of a two-dimensional grid, and a single boundary's protection cannot be turned into a global error guarantee.[3][6]

Knowledge Transfer

An encoder and a square 16-QAM label grid share the one-bit-neighbor structure, but their mechanisms differ: physical sampling at adjacent angular positions versus nearest-neighbor symbol confusion in a signal constellation. Live Encoding And Decoding can describe a complete encoder/readout pipeline, yet the static Gray ordering need not include that pair or a channel. Live Error-Correcting Code is not its genus because Gray adjacency is not redundant recovery. A cross-domain minimal-change-traversal skeleton is a future-prime question, while this binary-word identity remains domain-specific.[4][5]

Neighborhood in Abstraction Space

Gray Code sits in a moderately populated region (46th percentile for distinctiveness): it has near-neighbors but no dense thicket of look-alikes.

Family — Codes, Matrices & Combinatorial Problems (33 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14

References

[1] Paul E. Black, “Gray code,” NIST Dictionary of Algorithms and Data Structures, Definition and Note, updated 2020. registry ↩a ↩b

[2] Frank Gray, “Pulse Code Communication,” U.S. Patent 2,632,058, filed 1947, issued 1953, original reflection construction and variants. registry ↩a ↩b

[3] “Transition Restricted Gray Codes”, Electronic Journal of Combinatorics 3(1), R11 (1996), path/cycle distinction. registry ↩a ↩b ↩c

[4] Analog Devices, “AN-2614: SSI Absolute Encoder Protocol Support for TMC8100”, Features, Table 1 and conversion section. registry ↩a ↩b

[5] MathWorks, “Symbol Mapping Examples”, 16-QAM Gray example. registry ↩a ↩b ↩c

[6] MathWorks, “Rectangular QAM Modulator Baseband”, odd-\(K\) cross-shaped constellation caveat. registry ↩a ↩b

[7] Analog Devices, “MT-020: ADC Architectures I: The Flash Converter”, pp. 11–12, Fig. 11. registry ↩