Skip to content
K Knidox Search…
Computer Science · Number Systems

Gray Code Converter

Convert between binary and reflected Gray code, both ways.

Direction
Binary 0101 → decimal 5
Try a value
Gray code
0111

Decimal value: 7.

Bit-by-bit — highlighted bits differ between the two codes

Binary
0101
Gray code
0111

Gray code is a binary ordering in which any two consecutive values differ by exactly one bit. To convert binary to Gray code, XOR each bit with the bit to its left: gray = B ⊕ (B >> 1). For example, binary 0101 (decimal 5) becomes Gray code 0111, whose decimal value is 7.

What Gray code is

Reflected Gray code, named after physicist Frank Gray, is an alternative way to order binary numbers so that stepping from one value to the next flips only a single bit. Ordinary binary counting can flip several bits at once — going from 3 (011) to 4 (100) changes all three low bits — which risks transient glitches when the bits do not update at exactly the same instant. Gray code sidesteps that by guaranteeing a one-bit change between every adjacent pair, which is why it is favored in hardware that reads a value while it is still moving.

gray = B ⊕ (B >> 1)

Each Gray bit is the binary bit XORed with the binary bit to its left; the most significant bit is copied unchanged.

Worked example: binary 0101

Take the binary value 0101 (decimal 5) and turn it into Gray code by XORing it with a copy of itself shifted one place to the right.

  1. 1
    Write the binary value. B = 0101, which is decimal 5.
  2. 2
    Shift a copy right by one place. B >> 1 = 0010, dropping the last bit and adding a leading 0.
  3. 3
    XOR the two rows column by column. 0101 ⊕ 0010: a column is 1 only where the bits differ, giving 0, 1, 1, 1.
  4. 4
    Read the Gray code. The result is 0111, whose decimal value is 7. The most significant bit is unchanged because it XORs with an implicit 0.

Binary and Gray code, 0 to 7

Each Gray code differs from the one above it in exactly one bit position.

DecimalBinaryGray codeBit changed vs previous
0000000
1001001bit 0
2010011bit 1
3011010bit 0
4100110bit 2
5101111bit 0
6110101bit 1
7111100bit 0

Where Gray code is used, and why it reflects

Because only one bit changes between adjacent values, Gray code is the standard encoding for rotary and linear position encoders, where a sensor reads a shaft angle that may fall on a boundary between two counts. With plain binary a boundary read could momentarily return a wildly wrong number as several bits settle; with Gray code the worst case is being off by one. The same one-bit property minimizes errors in Karnaugh maps and reduces switching activity in some digital circuits. The name reflected comes from how the sequence is built: to extend an n-bit table to n+1 bits, mirror the existing list, prefix 0 to the original half and 1 to the reflected half — the mirror symmetry is what preserves the single-bit-change rule across the join.

What is Gray code used for?
Gray code is used in rotary and linear position encoders, Karnaugh maps, and some communication and error-correction schemes. Anywhere a value is read while it changes, the guarantee that only one bit flips between adjacent numbers keeps a mid-transition read from producing a large error.
Why does only one bit change between values?
That single-bit-change property is the definition of Gray code, and the reflected construction preserves it: mirroring the table and prefixing 0 to one half and 1 to the other means each step across the sequence — including the mirror join — flips exactly one bit.
How do I convert binary to Gray code?
XOR the binary number with itself shifted right by one bit: gray = B ⊕ (B >> 1). Equivalently, copy the most significant bit, then set each remaining Gray bit to the XOR of the binary bit above it and the binary bit itself.
How do I convert Gray code back to binary?
Copy the most significant bit, then take a running XOR: each binary bit equals the Gray bit at that position XORed with the binary bit you just computed to its left. So b[i] = g[i] ⊕ b[i−1], working from the most significant bit down.
Is 0111 the Gray code for 5 or the value 7?
Both readings are valid depending on context. As the Gray code of decimal 5 (binary 0101) the pattern is 0111; read as a plain binary number, 0111 equals 7. The converter shows both the bit pattern and its decimal value so the distinction is clear.
What does “reflected” mean in reflected Gray code?
It describes how the sequence is generated. To go from n bits to n+1, you write the n-bit list, then write it again in reverse (reflected), prefix 0 to the first copy and 1 to the reflected copy. This mirroring is what keeps consecutive codes one bit apart.