How to use the Binary to Gray code tool
- Enter the value in the Binary box. Include leading zeros if the code word has a fixed width.
- Select Convert to apply the Gray code rule to the whole word.
- Read the Gray code in the Result box. It has exactly as many bits as you entered.
- Select Copy to take the code word, or Example to encode 101101.
What Gray code is for
In ordinary binary, stepping from 7 to 8 changes four bits at once: 0111 becomes 1000. If those bits are read while they are changing, a sensor or a circuit can see almost any value. Gray code orders the patterns so that consecutive numbers differ in exactly one bit. The Gray codes for 7 and 8 are 0100 and 1100.
The code is named after Frank Gray of Bell Labs, whose patent on it was granted in 1953. The usual version is called reflected binary code because each longer table is built by mirroring the shorter one.
The binary to Gray code rule
The conversion is one operation: XOR the number with itself shifted right by one bit. Read bit by bit, that means the leftmost bit is copied, and every other Gray bit is the XOR of the binary bit in that position and the binary bit to its left.
For 101101: copy the first 1. Then 1 xor 0 = 1, 0 xor 1 = 1, 1 xor 1 = 0, 1 xor 0 = 1 and 0 xor 1 = 1. The Gray code is 111011.
101101 -> 111011
0111 -> 0100
1000 -> 1100
11111111 -> 10000000The 3-bit table
Running every 3-bit value through the tool gives the standard sequence. Read down the right column and you will see one bit change per row, including from the last row back to the first.
000 -> 000
001 -> 001
010 -> 011
011 -> 010
100 -> 110
101 -> 111
110 -> 101
111 -> 100Input handling
The tool treats the input as an unsigned bit pattern of up to 65,536 bits. Whitespace between groups is ignored and one leading 0b is removed. The result is padded with zeros to the input length, so 0101 gives 0111 and not 111. There is no minus sign and no separate width field.
Where developers meet Gray code
Gray code turns up wherever a multi-bit value is sampled without a shared clock or while it moves.
- Absolute rotary and linear encoders, where the tracks are read mechanically or optically.
- Read and write pointers of asynchronous FIFOs that cross clock domains.
- Karnaugh maps, whose rows and columns are labeled in Gray order.
- Symbol mapping in QAM and PSK modulation, so that a small noise error flips a single bit.
Frequently asked questions
What is the formula for converting binary to Gray code?
Gray = Binary XOR (Binary shifted right by one). In C-like syntax that is g = b ^ (b >> 1). The top bit stays the same and each lower bit is the XOR of two neighboring binary bits.
Does the Gray code have the same number of bits as the binary input?
Yes. This tool pads the result to the input length, so an 8-bit word always returns an 8-bit code. The leading bit of the output always equals the leading bit of the input.
How do I convert Gray code back to binary?
Use the Gray code to Binary tool. Decoding copies the top bit and then XORs each Gray bit with the binary bit just produced, so 111011 returns 101101.
Can I do arithmetic on Gray code values?
Not directly. Gray code is not a weighted code, so its bit positions have no fixed place values. Convert to binary, do the arithmetic, and convert the result back.
Why does 11111111 become 10000000?
Every bit after the first is the XOR of two neighboring 1 bits, which is 0. Only the copied top bit remains set. It is also the last code in the 8-bit sequence, one bit away from 00000000.