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 arranges distinct fixed-width binary words so that each required transition between neighboring entries changes exactly one bit. For a complete \(n\)-bit binary Gray code, all \(2^n\) words appear once. An ordinary linear listing requires the condition between successive entries; a cyclic Gray code also requires the last and first words to differ in one bit. In graph terms the complete linear listing is a Hamiltonian path of the \(n\)-dimensional cube, and a cyclic listing closes that path into a Hamiltonian cycle.[1][2]

The familiar reflected binary code is one construction, not the definition of every Gray code. From a shorter list, prefix the forward copy with 0 and its reverse with 1. The reversal makes the middle boundary differ only in the new leading bit, while inherited within-half boundaries retain their one-bit property. For three bits this yields $000,001,011,010,110,111,101,100\(; the final \$100\) and initial $000$ also differ in one bit. Frank Gray's original patent used this reflection process for pulse-code communication and expressly recognized secondary forms with the same neighboring-symbol property.[3]

This is a local transition guarantee, not a general shield against error. When an apparatus passes from one adjacent encoded state to the next and only their single differing signal is ambiguous, a readout can be the old or new valid label rather than an arbitrary mixed multi-bit label. Faulty sensors, nonadjacent jumps, asynchronous motion across several boundaries, or noisy communication can still cause incorrect readings. A Gray code introduces no redundant separation between valid words and therefore is not an error-correcting code.[1][4]

Structural Signature

Sig role-phrases: declared state set → operational adjacency → unique binary labels → one-bit boundary → traversal or assignment rule → readout and failure scope.

  • Declared state set. Specify the binary width and the states to be covered. A complete \(n\)-bit code lists every possible \(n\)-bit word once; a restricted Gray labeling can cover only a stated subset or mapped state space. Without this declaration, “all states” is untestable.[1][2]
  • Operational adjacency. Decide which transitions matter. In a one-dimensional count they are successive entries; in a rotary encoder the final-to-first transition matters too. A square constellation has horizontal and vertical nearest-neighbor edges rather than one natural single traversal.[1][5]
  • Unique binary labels. Each represented state receives a distinct fixed-width word. If labels repeat, the readout loses state identity even though some transitions may remain one-bit.[1]
  • One-bit boundary. For each required adjacent pair \(x,y\), the Hamming distance \(d_H(x,y)\) is exactly one. This is the defining invariant, not an incidental reduction in average bit flips.[1][3]
  • Traversal or assignment rule. Reflection-and-prefix generates one full cyclic code; other valid Gray paths or application-specific placements exist. The construction is variable, but an order or adjacency-respecting assignment is needed to test the invariant.[3][2]
  • Readout and failure scope. Applied benefits depend on how the labels are sensed or decoded. Single-boundary ambiguity in an encoder and nearest-neighbor symbol confusion in a square 16-QAM grid are different error models; neither makes arbitrary errors disappear.[4][5]

For standard reflected binary code, the word at binary index \(b\) is \(g=b\oplus(b\mathbin{\mathrm{>>}}1)\), with \(\oplus\) denoting bitwise XOR; inverse recovery takes cumulative XOR from the most significant Gray bit downward. That formula identifies this particular construction and must not be imposed on every Gray ordering.[3][6]

What It Is Not

It is not ordinary binary counting. The natural-binary transition \(0111\to1000\) changes four bits, whereas a Gray ordering keeps each declared adjacent transition at one. It is not just any permutation of the binary words: uniqueness and completeness alone say nothing about Hamming distance between neighbors. Nor is every Gray code the reflected construction; Gray's patent describes variants, and combinatorial work studies other paths through the same cube.[3][2]

It is not error correction. Adjacent valid words are deliberately close, not separated by redundant distance for recovering an arbitrary corrupted word. It is not one universal formula: \(b\oplus(b\mathbin{\mathrm{>>}}1)\) is the reflected binary index map. It is also not identical to a Gray labeling of an arbitrary constellation: a square 16-QAM map combines Gray orders along two axes so nearest grid neighbors differ by one bit, whereas one linear Gray list has one predecessor and successor per interior entry.[5][7]

Finally, “one bit changes” concerns designed neighboring states or labels, not a guarantee that every physical transition is hazard-free. If several state boundaries are crossed before sampling, or independent noise flips another bit, the condition no longer bounds the outcome to two adjacent labels.[1][4]

Scope of Application

In combinatorics and computing, a complete binary Gray code enumerates all bit vectors by minimal changes. Its graph interpretation makes the completeness and cycle questions explicit. Reflected binary code is a compact recursive generator; nonreflected Gray paths can satisfy the same local invariant. Broader combinatorial Gray codes enumerate permutations, subsets or other objects under a specified “small change” relation, but those are a generalization over different adjacency graphs rather than exact synonyms for this binary-word entry.[2]

In position sensing, adjacent angular sectors can be assigned Gray labels so a mechanical boundary changes one track instead of several. NIST describes this motivation; Analog Devices documents an actual synchronous-serial absolute encoder field transmitted in Gray code and then converted to binary. In analog-to-digital conversion, a Gray-like intermediate code can constrain the effect of one metastable comparator at a transition under specified circuit assumptions.[1][6][4]

In digital modulation, Gray labels can be assigned to physically adjacent constellation points. For MathWorks' square 16-QAM example, horizontal and vertical nearest neighbors differ in one bit. This is a coordinate-wise transfer of Gray ordering, not proof that every constellation admits perfect Gray labeling or that all errors become single-bit. MathWorks documents a cross-shaped odd-\(K\) rectangular QAM case where some nearest pairs differ in two bits.[5][7]

Clarity

The word “code” hides two levels that should be separated. A binary Gray sequence is an ordered mathematical object with the one-bit-adjacency invariant. A Gray-labeled device or constellation assigns such labels to the neighboring states that matter for a use. The application chooses the adjacency graph, whether closure matters, and how errors arise. A specific label sequence can be valid for one graph and fail for another.

For example, the reflected three-bit list $000,001,011,010,110,111,101,100$ is complete and cyclic. The transition \(010\to110\) flips only its most significant bit. By contrast, reading across a row of a square QAM grid is not the same as marching through the entire constellation in one path; the two axes each carry a smaller Gray order. Those distinctions prevent “Gray code” from being treated as a magic word for any low-error mapping.[3][5]

Manages Complexity

The local rule compresses a large ordering problem into an edge check: for each required adjacency, compare the two labels and count differing bits. Recursive reflection supplies a systematic full-list construction without memorizing all \(2^n\) words. A hardware designer can then reason about each position boundary, and a modulation designer can reason about the nearest-neighbor graph, rather than enumerate every possible readout error as if all were equally likely.[3][1][5]

The compression is conditional. A cyclic encoder must check its wraparound boundary, not only interior entries. A complete code must check uniqueness and coverage as well as Hamming-one steps. And a graph with extra adjacency edges can be harder to label: a linear sequence has at most two neighbors per interior position, while a QAM grid point can have four. The local invariant helps only on edges that the assignment actually covers.[2][7]

Abstract Reasoning

Represent each \(n\)-bit word as a vertex of the \(n\)-cube and connect two vertices when their Hamming distance is one. A complete Gray code traces a vertex-once path along those edges; cyclic closure adds one more edge from last to first. This makes a crucial negative inference easy: a list that changes one bit for most steps but misses one word, repeats a word or makes one two-bit jump is not a complete Gray code under the stated definition.[2]

When applying it, first choose the operational adjacency graph. A rotary encoder's graph is cyclic position order. A 16-QAM grid's relevant graph connects horizontal and vertical nearest symbols. Next ask whether an injective bit-label assignment maps every required edge to a Hamming-one edge. Then state the error model: a one-step boundary ambiguity, a nearest-neighbor noise error or something broader. One-bit adjacency bounds only errors constrained to the checked graph edge; it does not say that a noisy channel cannot land at a distant state.[1][5][7]

Knowledge Transfer

The structure transfers from an angular encoder to a square QAM constellation because both choose an adjacency relation among states and make each required local transition differ in one label bit. The useful consequence differs. In the encoder, asynchronous sensing at one physical boundary is less likely to synthesize a far-away position. In 16-QAM, a demodulator that mistakes a symbol for one horizontal or vertical nearest neighbor changes one payload bit. The first concerns physical state sampling; the second concerns geometric symbol confusion under a channel model. Neither entails the other's failure mode or a universal performance guarantee.[6][4][5]

The broader portable skeleton is a minimal-change enumeration or labeling on a state-adjacency graph. Generalized combinatorial Gray codes ask that question for nonbinary objects too, which is a plausible future-prime question only after a cross-domain identity review. The named binary Gray code remains domain-specific here because fixed-width binary labels and Hamming-one transitions are constitutive. No existing live prime was found that strictly subsumes the complete identity under its full definition.

Examples

Absolute rotary position readout

An absolute encoder divides a rotation into identifiable positions. Under a Gray assignment, neighboring position words differ in one bit, including the wraparound boundary if cyclic coding is required. NIST notes the advantage over ordinary binary words when reading elements are slightly misaligned. Analog Devices' AN-2614 documents a concrete synchronous-serial encoder whose position field is received in Gray form and converted to ordinary binary by the interface. The conversion happens after readout; it is not what creates the one-bit boundary property.[1][6]

The declared state set is the encoder's discrete angular positions and fixed-width position field. Operational adjacency is the next position around the ring, including last-to-first if the ring uses a complete cyclic assignment. Unique binary labels make each reported position distinguishable. The one-bit boundary means the designed neighbor transition toggles one position bit. The traversal or assignment rule is the encoder's position-to-Gray mapping, followed by readout conversion. The readout and failure scope is ambiguity at one position boundary: if just the transitioning bit is uncertain, either old or new label can be sampled, but track failure, electrical noise or motion crossing several sectors is not cured.[1][6][4]

Mapped back: This is a direct use of a Gray ordering to constrain a particular adjacent-state readout failure, not an error-correcting code and not a universal promise of accurate sensing.

A square 16-QAM label grid

MathWorks' square 16-QAM example assigns distinct four-bit labels to its sixteen points. It uses a two-bit Gray order on the in-phase axis and another on the quadrature axis. Move one horizontal or vertical grid step and only the corresponding axis's Gray component changes by one bit. The grid is not a single linear Hamiltonian traversal; it is a two-dimensional Gray labeling derived from one-dimensional orders. A nearest-neighbor symbol decision error therefore changes one bit in this example. Diagonal or farther errors need not, and a cross-shaped odd-\(K\) constellation can have nearest pairs differing by two bits.[5][7]

The declared state set is the sixteen constellation points and four-bit labels. Operational adjacency is horizontal or vertical nearest-neighbor geometry. Unique binary labels assign one word per symbol. The one-bit boundary is satisfied on those grid edges. The traversal or assignment rule composes row and column Gray orders rather than listing all points once in one path. The readout and failure scope is a demodulator confusing one symbol with a checked nearest neighbor; more distant noise events lie outside that one-bit conclusion.[5][7]

Mapped back: The minimal-change invariant has transferred to a different adjacency graph and error mechanism while retaining its formal edge test. Calling the entire 16-QAM map a single binary Gray sequence would blur an important structural difference.

Structural Tensions

Local transition safety versus global error protection. One-bit adjacency prevents an ambiguous single designed boundary from mixing several changing signals, but it adds no redundancy and cannot reconstruct a word arbitrarily corrupted by noise. Diagnostic: Does the actual failure mechanism confuse only two adjacent states, or can it flip independent tracks or jump farther?[1][4]

Seen in practice: Adjacent-state readout in tension with redundant error detection

Complete coverage versus constrained adjacency. Listing all \(2^n\) binary words uniquely while making each step Hamming-one is stronger than merely displaying several one-bit examples. A cyclic use adds a last-to-first condition; extra transition restrictions may constrain possible constructions. Diagnostic: Are uniqueness, coverage, all interior edges and required closure checked?[2]

Portable Gray order versus application-specific neighbor geometry. A reflected linear/cyclic order works for sequential states, while a two-dimensional constellation declares more neighbor edges. Some layouts cannot receive the same perfect one-bit nearest-neighbor promise under a chosen mapping. Diagnostic: Which physical or logical state edges are actually relevant, and do all of those edges—not just a selected path—carry one-bit differences?[5][7]

Structural–Framed Character

  • Evaluative weight: “Gray” names a verifiable local code property, not blanket superiority. Benefit depends on the application's transition and error model.
  • Human-practice dependence: Designers choose state order and bit assignment, but the Hamming-one condition can be checked mathematically once those choices are declared.
  • Institutional origin: Frank Gray's patent is historically important, while the code property and other constructions are not constituted by that patent; NIST and engineering tools describe later uses.[3][1]
  • Vocabulary travel: The term extends to nonbinary combinatorial objects and to constellation Gray labeling. Those uses share a minimal-change idea but require their own declared adjacency, not an automatic claim of binary reflected-code identity.[2][5]
  • Import versus recognition: A sequence is recognized by unique labels and every required Hamming-one neighbor edge. Calling a natural binary counter or an arbitrary communication code “Gray” without that edge check only imports the label.

Its character: structural and formal within binary coding and combinatorial generation, yet domain-specific as named. Its exact recognition test uses binary words and one-bit Hamming adjacency; engineering benefits are contingent overlays.

Structural Core vs. Domain Accent

The portable skeleton is minimal-change traversal or labeling of a graph of states. The domain-bound mechanism fixes states as binary words, with Hamming distance one on every required neighbor edge and uniqueness/coverage claims stated separately. Reflected recursion, encoder tracks and 16-QAM mappings are application overlays or particular constructions, not the core identity.[3][2]

This entry does not assert a prime-level “minimal-change code” already exists. Generalized combinatorial Gray codes do suggest a future-prime question about whether the same structure is autonomously recognized across object types and application domains. The current live catalog offers no strict parent whose full definition applies to every binary Gray code. Prime Permutation allows arbitrary rearrangements, Prime Encoding And Decoding requires a coordinated pair across a channel/store, and Error-Correcting Code requires redundant recoverability; their topical proximity is not DAG subsumption.

No strict parent edge is staged. Permutation is related because a complete Gray sequence can be specified as a reordering of all binary words, but the code is the resulting adjacency-constrained ordered object, not necessarily an operation that reassigns a pre-existing arrangement. Encoding And Decoding can be instantiated when a physical encoder maps positions to Gray words and a controller decodes them, but a mathematical Gray sequence need not have channel, decoder or round trip. Error-Correcting Code is not a parent: Gray words do not acquire redundancy or correction distance by being adjacent. Modulation is a use setting for Gray constellation labels, not the code's genus.[6][5]

Neighborhood in Abstraction Space

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

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

Nearest neighbors

Computed from structural-signature embeddings · 2026-10-08

Not to Be Confused With

  • Natural binary count: some adjacent numeric values change multiple bit positions; \(0111\to1000\) is the simple counterexample.
  • Reflected binary code: Gray's recursive, cyclic construction and the \(b\oplus(b\mathbin{\mathrm{>>}}1)\) transform; one member of the broader Gray-code family, not its exhaustive definition.[3]
  • Cyclic Gray code: a stricter case in which the final and initial labels also differ in one bit. A complete linear Gray path need not close.[2]
  • General combinatorial Gray code: applies a declared minimal-change adjacency to other objects such as permutations or subsets, not necessarily to fixed-width binary words.[2]
  • Error-correcting code: typically uses redundant separated codewords and decoding under an error model; one-bit-neighbor words are close by design and provide no such general recovery guarantee.
  • A universal one-bit constellation map: square 16-QAM admits the demonstrated nearest-neighbor property, but MathWorks' cross-shaped odd-\(K\) QAM caveat shows why the property cannot be presumed for every layout.[5][7]

References

[1] Paul E. Black, “Gray code,” NIST Dictionary of Algorithms and Data Structures, Definition and Note, updated 2020. Authoritative definition and mechanical-encoder application; its broad cycle phrasing is narrowed here by the mathematical path/cycle distinction. registry ↩a ↩b ↩c ↩d ↩e ↩f ↩g ↩h ↩i ↩j ↩k ↩l ↩m ↩n

[2] “Transition Restricted Gray Codes”, Electronic Journal of Combinatorics 3(1), R11 (1996), abstract. Original mathematical definition of a Gray code as a Hamiltonian path and a cyclic code as a Hamiltonian cycle. registry ↩a ↩b ↩c ↩d ↩e ↩f ↩g ↩h ↩i ↩j ↩k ↩l

[3] Frank Gray, “Pulse Code Communication,” U.S. Patent 2,632,058, filed 1947, issued 1953, Description of reflected binary coding and its alternative forms. Original patent. registry ↩a ↩b ↩c ↩d ↩e ↩f ↩g ↩h ↩i ↩j

[4] Analog Devices, “MT-020: ADC Architectures I: The Flash Converter”, pp. 11–12, Fig. 11. Original technical account of conditional Gray-readout metastability advantage. registry ↩a ↩b ↩c ↩d ↩e ↩f ↩g

[5] MathWorks, “Symbol Mapping Examples”, “Gray Encode Modulated Signal.” Original 16-QAM label-grid example and one-bit adjacent-point description. registry ↩a ↩b ↩c ↩d ↩e ↩f ↩g ↩h ↩i ↩j ↩k ↩l ↩m ↩n

[6] Analog Devices, “AN-2614: SSI Absolute Encoder Protocol Support for TMC8100”, Features, Synchronous Serial Interface Protocol, Table 1 and “Convert Gray Code to Binary Code.” Original vendor implementation documentation. registry ↩a ↩b ↩c ↩d ↩e ↩f

[7] MathWorks, “Rectangular QAM Modulator Baseband”, “Constellation ordering” note for odd \(K\). Original documentation of cross-shaped layouts with some two-bit nearest-neighbor pairs. registry ↩a ↩b ↩c ↩d ↩e ↩f ↩g ↩h