How to use the Decimal to Gray code tool
- Enter a non-negative whole number in the Decimal box, for example 45.
- Select Convert to encode it.
- Read the Gray code bits in the Result box. Leading zeros are not added.
- Select Copy to copy the code. Pad it on the left with zeros if your encoder or counter uses a fixed width.
What Gray code is
Gray code, formally the reflected binary code, orders binary words so that two consecutive values differ in exactly one bit. Ordinary binary does not have that property. Going from 7 to 8 changes 0111 to 1000, four bits at once. In Gray code the same step goes from 0100 to 1100, a single bit.
The code is named after Frank Gray of Bell Labs, who described it in a patent granted in 1953. A sensor or circuit that reads the bits while they are changing can be off by at most one step, never by a wild value.
How the decimal to Gray code conversion works
The tool first reads the decimal input as an integer n. It then computes n XOR (n shifted right by one bit). In effect each Gray bit is the XOR of a binary bit and the bit to its left, and the most significant bit is copied unchanged.
For 45, binary is 101101. Shifted right by one it is 010110. XOR of the two rows is 111011.
n = 101101 (45)
n >> 1 = 010110
XOR = 111011 (Gray code)Gray code table for 0 to 15
These are the tool's outputs for the first sixteen integers. Read each as a 4-bit word by adding zeros on the left, and every row differs from the one before it in one position. The sequence also wraps: 15 (1000) and 0 (0000) are one bit apart.
0 -> 0 8 -> 1100
1 -> 1 9 -> 1101
2 -> 11 10 -> 1111
3 -> 10 11 -> 1110
4 -> 110 12 -> 1010
5 -> 111 13 -> 1011
6 -> 101 14 -> 1001
7 -> 100 15 -> 1000Input limits and typical uses
The input must be a whole number of at most 4,096 digits made of 0 to 9. Negative numbers return Enter a non-negative value, and decimal points, commas or inner spaces are reported as invalid digits. Leading zeros are ignored, so 007 gives 100.
Engineers use the result to label positions on absolute rotary encoders, to order the rows and columns of Karnaugh maps, to pass counter values between clock domains in asynchronous FIFOs, and to map bits to symbols in QAM modulation so that a neighboring-symbol error costs one bit.
Frequently asked questions
What is 45 in Gray code?
45 in Gray code is 111011. Decimal 45 is 101101 in binary, and XORing it with itself shifted right one place gives 111011.
What is the Gray code for 7 and 8?
7 is 100 and 8 is 1100, or 0100 and 1100 as 4-bit words. They differ only in the top bit, whereas binary 0111 and 1000 differ in all four.
Does the output have a fixed number of bits?
No. The converter prints the shortest form, without leading zeros. If you need 8 or 10 bits, pad on the left with zeros. Padding does not change the value, because leading zeros stay zero under the Gray transform.
Is Gray code a weighted code?
No. In binary each position has a weight of 1, 2, 4 and so on. Gray code bits have no fixed weights, so you cannot add place values to read it. Convert back with the Gray code to Decimal tool.
Can I convert a negative number to Gray code?
This tool rejects negative input. Reflected Gray code is defined for unsigned values. For a signed quantity, offset it into a non-negative range first, as encoder firmware usually does.
Why do rotary encoders use Gray code?
Mechanical or optical tracks never switch at exactly the same instant. With binary, a read during a multi-bit transition can return a position far from the true one. With Gray code only one track changes per step, so the reading is either the old or the new position.