How to use the Gray code to Decimal tool
- Type the encoder reading or other Gray code word into the Gray code box, for example 111011.
- Select Convert. The tool decodes the word to binary internally and then to base 10.
- Read the decimal integer in the Result box.
- Select Copy to copy the number, or Clear to enter the next reading.
From Gray code to a decimal number
Gray code has no place values, so you cannot add up weights the way you would for binary. The conversion takes two stages. First the Gray word is decoded to ordinary binary by a running XOR from the left. Then the binary number is evaluated with the usual weights 1, 2, 4, 8 and so on.
For 1101: the binary bits are 1, then 1 XOR 1 = 0, then 0 XOR 0 = 0, then 0 XOR 1 = 1. Binary 1001 is 8 + 1, so the answer is 9.
Gray code to decimal table for 4 bits
Each row was produced by this tool. Reading down the left column and then the right, every code differs from its predecessor in a single bit, and the last entry, 1000, is one bit away from the first.
0000 -> 0 1100 -> 8
0001 -> 1 1101 -> 9
0011 -> 2 1111 -> 10
0010 -> 3 1110 -> 11
0110 -> 4 1010 -> 12
0111 -> 5 1011 -> 13
0101 -> 6 1001 -> 14
0100 -> 7 1000 -> 15Reading an absolute encoder
A common use is turning an encoder reading into a position. A 4-bit absolute rotary encoder has 16 positions of 22.5 degrees each. If the tracks read 1101, the position is 9, so the shaft sits at 9 x 22.5 = 202.5 degrees. An 8-bit encoder has 256 positions, about 1.41 degrees apart, and the reading 10000000 is position 255, the last step before the code wraps to zero.
Check the bit order before you convert. The tool expects the most significant track first. If your wiring or log lists the least significant bit first, reverse the string.
Input handling
The Gray code box accepts 1 to 65,536 binary digits. Spaces, line breaks and a leading 0b are ignored. Leading zeros do not change the decimal result: 000111 and 111 both give 5. The output is a plain non-negative integer with no separators, computed with arbitrary-precision arithmetic, so very long words are converted without rounding.
Frequently asked questions
What is Gray code 111011 in decimal?
It is 45. The Gray word decodes to binary 101101, and 32 + 8 + 4 + 1 equals 45.
Can I convert Gray code to decimal by adding place values?
No. Gray code is an unweighted code. Adding powers of two gives the wrong number: 1101 would read as 13 in binary, but as Gray code it is 9. Decode to binary first.
What decimal value is Gray code 1111?
1111 is 10. Decoding gives binary 1010. A word of all ones in Gray code always decodes to alternating bits starting with 1.
Does it matter how many leading zeros my Gray code has?
Not for the decimal value. Leading zeros decode to leading zeros, which add nothing. Use the Gray code to Binary tool if you need the decoded word at its original width.
Does this work for excess Gray codes used by some encoders?
Partly. Encoders whose position count is not a power of two often use an excess Gray code, a slice from the middle of a longer sequence. This tool decodes the standard reflected code and applies no offset, so subtract the offset given in the device documentation from the result.
How do I go the other way, from decimal to Gray code?
Use the Decimal to Gray code tool. It XORs the number with itself shifted right by one bit. Converting 45 there returns 111011, the input used in this page's example.