How to use the gray code converter
- Choose what your input is: a decimal number, a binary number or a Gray code written as bits.
- Enter the value. Binary and Gray code can be up to 64 bits, and leading zeros are kept.
- Read the decimal value, the binary form and the Gray code, with the Gray code also as a decimal number.
- Choose a table width in the advanced options to see every Gray code for that number of bits and which bit changes at each step.
Formula
binary to Gray: gray = n XOR (n shifted right by 1)
Gray to binary: keep the first bit, then each binary bit = previous binary bit XOR the Gray bit
neighbors n and n + 1 differ in exactly one bit of their Gray codes
XOR gives 1 where two bits differ and 0 where they are the same. Gray code is also called reflected binary code.
The number 10 and a 4-bit table
Decimal 10 is 1010 in binary. Shifting right by one gives 0101, and 1010 XOR 0101 = 1111, so the Gray code of 10 is 1111. Going the other way, the Gray code 1111 gives binary 1010, which is decimal 10. The Gray code of 7 is 100, of 255 is 10000000 and of 12345 is 10100000100101.
The 4-bit sequence runs 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, 1000. In the table, only one bit changes from each row to the next, and the last code differs from the first by a single bit as well. All of these were checked against a Python script.
Why Gray code is used
In ordinary binary, going from 3 to 4 changes three bits at once (011 to 100). If the bits are read by sensors that do not switch at exactly the same instant, a reading in between can be badly wrong. A Gray code changes one bit at a time, so any reading caught in the middle of a change is off by at most one step. That is why rotary encoders, position sensors and some counters use it.
Other uses and related tools
The same pattern appears in puzzles such as the Towers of Hanoi, in the order of cells in a Karnaugh map and in some error reduction schemes. For plain binary arithmetic, use the Binary Calculator, for the XOR and shift operations the Bitwise Calculator, and for other bases the Number Base Converter.
More tools for playing with numbers
For more number patterns and conversions, try the Digit Grouping Converter, Magic Square Generator and Pythagorean Triples Generator.
Assumptions and limits
- The code is the standard reflected binary Gray code.
- Binary and Gray inputs are strings of 0 and 1 of up to 64 bits, and decimal inputs have up to 20 digits.
- Numbers are treated as unsigned, with no negative values.
- The table covers 1 to 6 bits, which is up to 64 rows.
- The value in the form is an example to replace with your own.
Frequently asked questions
Is there only one Gray code?
No. Any sequence where neighbors differ by a single bit is a Gray code. The reflected binary version is the one in common use, and it is the one this tool produces.
Why is it called reflected?
The sequence for n + 1 bits is the sequence for n bits, then the same sequence in reverse order with a leading 1. The second half is a mirror image of the first.
Can I add Gray codes directly?
Not simply. Convert to binary, do the arithmetic and convert back. The codes are useful for reading positions, not for adding.
What does the last column of the table show?
It shows which bit position changed compared with the row above, counted from 0 at the right. It is a way to check that exactly one bit changes at each step.