How to use the Gray code to Binary tool
- Enter the Gray code bits in the Gray code box, for example 111011.
- Select Convert to decode the word.
- Read the binary value in the Result box. It has the same number of digits as the input.
- Select Copy to copy the binary word, or Example to load 111011 again.
How Gray code to binary decoding works
Decoding runs from the most significant bit to the least. The first binary bit equals the first Gray bit. Every later binary bit is the XOR of the Gray bit in that position and the binary bit just produced to its left. Put differently, each binary bit is the XOR of all Gray bits from the left edge down to its own position.
Encoding needs one XOR per bit using neighbors of the original word. Decoding is a running XOR, so a change in a high Gray bit ripples through every lower binary bit. The tool computes the same result by XORing the word with itself shifted right by 1, 2, 3 and further places until nothing is left.
Step-by-step example: 111011
Number the bits from 5 on the left down to 0 on the right, with g for Gray and b for binary. The decoded word 101101 is decimal 45.
Gray 1 1 1 0 1 1
Binary 1 0 1 1 0 1
b5 = g5 = 1
b4 = b5 XOR g4 = 1 XOR 1 = 0
b3 = b4 XOR g3 = 0 XOR 1 = 1
b2 = b3 XOR g2 = 1 XOR 0 = 1
b1 = b2 XOR g1 = 1 XOR 1 = 0
b0 = b1 XOR g0 = 0 XOR 1 = 1Width, leading zeros and input rules
The output is padded to the length of the cleaned input, so fixed-width words stay aligned. 0011 decodes to 0010, and 000111 decodes to 000101. Spaces and a leading 0b are removed first and do not count toward the width: 11 10 is treated as 1110 and returns 1011.
The box accepts 1 to 65,536 digits of 0 and 1. The tool assumes the standard reflected binary Gray code. Other single-step codes, such as balanced Gray codes or Excess-3 Gray, use different mappings and will not decode correctly here.
Gray code in clock domain crossing and encoders
Hardware designers decode Gray code constantly. In an asynchronous FIFO, the read and write pointers are converted to Gray code before they cross into the other clock domain, because a synchronizer that samples mid-transition can then be wrong by one count at most. On the far side the pointer is decoded back to binary for arithmetic, which is exactly this conversion.
Absolute rotary and linear encoders output Gray code for a similar reason, and firmware decodes each reading to binary before scaling it to an angle or distance.
Frequently asked questions
What is Gray code 111011 in binary?
It is 101101. The top bit is copied, and each following bit is the XOR of the Gray bit with the previous binary bit. The value is 45 in decimal.
What does Gray code 1000 decode to?
1000 decodes to 1111, which is 15. In a 4-bit Gray sequence 1000 is the last code word, one bit away from 0000, the first.
Why does the binary output keep leading zeros?
The converter preserves the width of the word you entered. Encoders and FIFO pointers have a fixed number of bits, and keeping the zeros makes the result line up with the input bit for bit.
How do I decode Gray code in C or Verilog?
In C, start with b = g and repeat while (g >>= 1) b ^= g. In Verilog, a common form is b[i] = ^(g >> i), a reduction XOR of the upper bits. Both match the output of this tool.
Can two different Gray codes give the same binary value?
No. The mapping is one-to-one for a given width. Every n-bit Gray word corresponds to exactly one n-bit binary word, so decoding followed by encoding returns the original bits.
What happens if one Gray bit is read incorrectly?
The error spreads to that bit and every lower bit of the decoded binary, because each binary bit depends on all Gray bits above it. A wrong top bit inverts the whole word: 0100 decodes to 0111, while 1100 decodes to 1000.