Gray code converter

Convert binary numbers to Gray code or Gray code back to binary, with the XOR steps shown for each bit. Gray code, also called reflected binary code, counts so that only one bit changes between neighbors. This page covers the binary code, not the hospital alert or gray color codes.

Show the working
Gray code table
0 numbers

Runs in your browser. Nothing you type is uploaded.

How to use the Gray code converter

  1. Pick Binary to Gray or Gray to binary in Convert.
  2. Set "Numbers as" to Decimal if you want to type ordinary numbers such as 5 or 12.
  3. Type one or more numbers, separated by spaces or new lines.
  4. Open "Show the working" for the XOR steps, or "Gray code table" for every value with 1 to 6 bits.

The Bits menu pads every result to the same width, which keeps a list aligned.

What Gray code is

In normal binary, going from 3 (011) to 4 (100) flips all three bits at once. In a real device those bits never flip at exactly the same moment, so for an instant a sensor might read 111 or 000. Gray code avoids this. Each step changes exactly one bit, so a reading is always either the old value or the new one. Frank Gray of Bell Labs described it in his "Pulse Code Communication" patent, filed in 1947, and it is named after him. Gray code is one of several ways to store numbers in bits, and the BCD converter covers another, where each decimal digit gets its own 4 bits.

3-bit Gray code compared with binary: 0 to 7 in Gray code is 000, 001, 011, 010, 110, 111, 101, 100. Each step changes only one bit, while binary changes up to three, such as 011 to 100.

How to convert binary to Gray code

Keep the first bit. Then each next Gray bit is the XOR of that binary bit and the one before it: 1 if they differ, 0 if they match, the same rule the XOR calculator applies to any two numbers. In code it is one line: gray = n ^ (n >> 1).

BitRuleGray
1Copy the first bit 00
20 XOR 11
31 XOR 01
40 XOR 11

So binary 0101 (5) is Gray code 0111.

How to convert Gray code to binary

Keep the first bit. Then each next binary bit is the XOR of the binary bit you just wrote and the next Gray bit. For Gray 0111: the first bit is 0, then 0 XOR 1 = 1, 1 XOR 1 = 0, 0 XOR 1 = 1, giving binary 0101. Going this way you have to work left to right, because each bit depends on the one before. The binary to decimal converter then shows that 0101 is 5.

4-bit Gray code table

DecimalBinaryGray
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000
Binary to Gray code for 0101: copy the first bit 0, then 0 XOR 1 is 1, 1 XOR 0 is 1 and 0 XOR 1 is 1, so the Gray code is 0111.

The table is called reflected because the second half mirrors the first. Take the 3-bit list, write it again in reverse order below, and put 0 in front of the top half and 1 in front of the bottom half. That gives the 4-bit list.

Where Gray code is used

  • Rotary encoders on motors and knobs, where the code wheel must never jump to a wrong position.
  • Karnaugh maps in digital logic, where neighboring cells differ by one variable.
  • Counters that pass values between clock domains in chips, since only one bit changes at a time.
  • Error correction in digital radio, where nearby symbols differ by one bit.

Frequently asked questions

What is 5 in Gray code?

0111. Binary 5 is 0101, and 0101 XOR 0010 is 0111.

Is Gray code the same as binary?

No. Both use 0 and 1, but Gray code orders the values so only one bit changes between neighbors.

Why is it called reflected binary code?

Because each longer table is built by mirroring the shorter one, as the table above shows.

Can I do arithmetic in Gray code?

Not directly. Convert to binary, do the math, then convert back.

Scroll to Top