Skip to the tool
Binary Translator Free tools

Gray code converter

Convert binary or decimal numbers to Gray code (reflected binary code), or Gray code back to binary, with every bit worked out.

Updated · Written and checked by Murugan Vellaichamy

Waiting for input

Spaces and a 0b prefix are fine.

Gray code
Try:

What is Gray code?

Gray code is a way of ordering binary numbers so that each number differs from the next in exactly one bit. Counting 3 to 4 in ordinary binary changes all three bits, 011 to 100; in Gray code it changes one, 010 to 110.

The name comes from Frank Gray of Bell Telephone Laboratories. His patent “Pulse code communication”, filed in 1947 and granted in 1953 as US patent 2,632,058, called it the reflected binary code, noting that it had as yet no recognised name.

Gray code table (4-bit)

DecimalBinaryGray codeBit that changed
000000000—
100010001bit 0
200100011bit 1
300110010bit 0
401000110bit 2
501010111bit 0
601100101bit 1
701110100bit 0
810001100bit 3
910011101bit 0
1010101111bit 1
1110111110bit 0
1211001010bit 2
1311011011bit 0
1411101001bit 1
1511111000bit 0

Read down the Gray code column: every step flips a single bit, and the last row, 1000, is also one bit away from the first, 0000, so the sequence wraps around.

How to convert binary to Gray code

  1. Copy the leftmost bit as it is.
  2. For every other bit, compare it with the bit to its left in the binary number. Write 1 if they differ and 0 if they match.

So 1011 becomes 1, then 1 vs 0 → 1, 0 vs 1 → 1, 1 vs 1 → 0, giving 1110. In code, this is one line: the Gray code is the number XOR the number shifted right by one bit, g = b ^ (b >> 1).

How to convert Gray code to binary

  1. Copy the leftmost bit.
  2. For each next bit, compare the Gray bit with the binary bit you just wrote. Write 1 if they differ and 0 if they match.

For 1110: write 1; 1 vs 1 → 0; 1 vs 0 → 1; 0 vs 1 → 1. The result is 1011, the number we started with. Note the difference from the other direction: here you compare with the binary bits you have written, not with the Gray bits.

Why is it called reflected binary code?

Take the 2-bit Gray code 00, 01, 11, 10. Write it again in reverse order underneath, so the list is mirrored. Put a 0 in front of the first half and a 1 in front of the mirrored half. The result is the 3-bit Gray code: 000, 001, 011, 010, 110, 111, 101, 100. Repeating the trick gives 4 bits, 5 bits and so on.

Why does changing one bit at a time matter?

When a value is read while it is changing, bits that should flip together may be caught at slightly different moments. In Gray code only one bit changes, so a reading taken mid-change is always one of the two neighbouring values.

Going from 011 to 100 in binary, a reading taken halfway could see any mix of the three bits, such as 111 (7) or 000 (0). Gray code is not used for arithmetic: to add or compare numbers, convert them back to binary first.

Sources and method

The converter uses the binary-reflected Gray code: gray = n XOR (n shifted right by one). The build checks that every pair of neighbours up to 4,096 differs in exactly one bit, and that the 3-bit code is the 2-bit code reflected.

Conversions use exact whole-number arithmetic (JavaScript BigInt), so long numbers keep every digit. Fractions are kept as exact ratios; when the digits repeat, the repeating block is found and marked instead of rounded.

Questions

Is Gray code the same as reflected binary code?

Yes. Reflected binary code is the name in Frank Gray’s patent; Gray code is the name it became known by. This converter uses that standard binary-reflected form.

What is 8 in Gray code?

1100. In binary 8 is 1000. Copy the 1; the next bit, 0, differs from the 1 to its left, giving 1; the last two bits match their neighbours, giving 00.

Can I convert decimal straight to Gray code?

Yes. Choose “Decimal → Gray code”. The converter turns the number into binary first and shows that step.

Does Gray code have a fixed width?

No. Any number of bits works. Pick a width to pad the result with leading zeros, which keep the same value in both binary and Gray code.

More converters

Copied