Gray Code Converter

Binary ⇄ reflected binary code, with the XOR that produces it and the reflection that explains it.

Direction

Binary → Gray is a single XOR per bit. Gray → binary is inherently sequential: each bit needs the binary bit already produced above it.

Binary value

Reflected-code table

Gray code

1110

Binary 1011 to Gray code

Each Gray bit is the XOR of the binary bit and the binary bit above it.

Binary 1011 to Gray code — columns bit i, b_i, b_(i+1), g_i = b_i XOR b_(i+1)
bit ib_ib_(i+1)g_i = b_i XOR b_(i+1)
310 (nothing above the MSB)1
2011
1101
0110

binary1011

Gray1110

whole-word formG = B XOR (B >> 1) = 1011 XOR 0101 = 1110

Warning:

This is the binary reflected Gray code. It is one Gray code among many: any single-bit-change ordering of the codewords is a Gray code, and other constructions give different tables.

Source: Frank Gray, US Patent 2,632,058, "Pulse Code Communication" (filed 1947, granted 1953)

The reflected construction

3-bit binary reflected Gray code

L1 = [0, 1]. For each extra bit, write the list, write it reversed below the mirror line, then prefix 0 to the top half and 1 to the bottom half.

3-bit binary reflected Gray code — columns bits, sequence
bitssequence
10, 1
200, 01, 11, 10
3000, 001, 011, 010, 110, 111, 101, 100

Consecutive codewords differ in exactly one bit, and so do the last and the first: the code is cyclic.

Warning:

This is the binary reflected Gray code. It is one Gray code among many: any single-bit-change ordering of the codewords is a Gray code, and other constructions give different tables.

Source: Frank Gray, US Patent 2,632,058, "Pulse Code Communication" (filed 1947, granted 1953)

Every 3-bit code, in order. Consecutive rows differ in exactly one bit, and so do the last and the first.
nbinaryGray code
0000000
1001001
2010011
3011010
4100110
5101111
6110101
7111100
Note:

Notation used on this page

  • The most significant bit is leftmost; bits are 0-indexed from the least significant end.
  • The width of the code is the length of the string you type; leading zeros are significant here.
  • This is the binary reflected Gray code. Other single-bit-change orderings exist and are also Gray codes.

Start from a worked example

What Gray code is

Gray code is a re-ordering of the same 2ⁿ bit patterns so that neighbouring entries differ in exactly one bit. Nothing is added and nothing is removed: all sixteen four-bit patterns appear in a four-bit Gray sequence, in a different order from counting.

Gray code is not a number system. It has no place values, you cannot add two Gray codewords digit by digit, and the codeword 1110does not “mean” 14 — it is position 11 in a sequence.

Binary to Gray

Each Gray bit is the XOR of the binary bit in the same column and the binary bit to its left: gᵢ = bᵢ ⊕ bᵢ₊₁, with the most significant bit copied through unchanged. For 1011: g₃ = 1, g₂ = 1 ⊕ 0 = 1, g₁ = 0 ⊕ 1 = 1, g₀ = 1 ⊕ 1 = 0, giving 1110.

As one whole-word operation that is g = b ⊕ (b ≫ 1), which is why the conversion is a single instruction in hardware and every column can be computed at the same time.

Gray to binary

The inverse is a prefix XOR and it runs from the most significant bit down: the top binary bit is the top Gray bit, and every bit after it is the Gray bit XORed with the binary bit just produced. For 1110: b₃ = 1, b₂ = 1 ⊕ 1 = 0, b₁ = 1 ⊕ 0 = 1, b₀ = 0 ⊕ 1 = 1, giving 1011.

This direction is sequential — each bit needs the one before it — which is why implementations use the logarithmic form b ^= b ≫ 1; b ^= b ≫ 2; b ^= b ≫ 4; … instead of a loop over the bits.

The reflection construction

The code’s real name is the reflected binary code, and the name is the construction. Start with 0, 1. To get the next width, write the list, then write it again in reverse, then prefix 0 to the first half and 1 to the second. The mirror line is where the one-bit-change property is preserved: the two entries either side of it differ only in the new leading bit.

Building the sequence by reflection, widths 1 to 4
WidthThe whole sequence, in order
L₁0, 1
L₂00, 01, 11, 10
L₃000, 001, 011, 010, 110, 111, 101, 100
L₄0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, 1000

Why one bit matters: shaft encoders and asynchronous crossings

A rotary encoder that reads plain binary can report any value at all while several tracks change at once. Going from 0111 to 1000 all four tracks flip, and if the sensors are read mid-transition the output can be anything from 0 to 15. With Gray code exactly one track changes per step, so a mid-transition reading is either the old position or the new one — the worst case is an error of one position.

The same argument covers a counter passed between two clock domains. If only one bit can change per increment, the sampling domain can never latch a value that was never a real count.

Gray code and Karnaugh maps

The K-map’s row and column labels are a Gray code. That is the whole reason adjacent cells combine. Labelling the axes 00 01 11 10 rather than 00 01 10 11 makes physically neighbouring cells differ in exactly one variable, which is what lets a pair of them collapse into a single product term. See the Karnaugh map solver.

The invariants

  • Consecutive codewords differ in exactly one bit.
  • The sequence is cyclic: the last codeword and the first also differ in one bit, so the code wraps.
  • Every one of the 2ⁿ patterns appears exactly once.
  • 0 and 1 are their own Gray codes; every larger power of two is not — 4 is 100 in binary and 110 in Gray.
  • The parity of a Gray codeword equals the parity of the number of ones in the binary value it encodes, because the XOR fold of b ⊕ (b ≫ 1) keeps the top bit and cancels each other bit exactly twice.

Other Gray codes exist

This page computes the binary-reflectedGray code (BRGC), which is what “the Gray code” almost always means. It is not the only code with the one-bit-change property; balanced Gray codes, monotonic Gray codes and non-Boolean Gray codes over other alphabets all exist. If a question says only “Gray code”, it means this one.

Width matters for the writing, not for the value

The Gray code of 5 is 111 at three bits and 00000111 at eight. It is the same codeword; only the padding differs. Leading zeros are part of the codeword. State the width.

Notation used on this page

  • Most significant bit first, left to right; bits are 0-indexed from the least significant end.
  • is XOR and is a logical right shift with zero shifted in.
  • The sequence table is indexed by the decimal value of the binary word, so row i holds the Gray codeword for i.

Sources

Frank Gray, Pulse Code Communication, US Patent 2,632,058 (opens in a new tab), filed 13 November 1947 and granted 17 March 1953. Gray himself called it the “reflected binary code”; the name “Gray code” was attached later by others.

Worked examples