Toolstia / Gray code converter
Gray code converter
Gray code is a way of counting where only one bit changes from each number to the next. Convert a binary number to Gray code or turn a Gray code back into binary, and see the full table for any size from 1 to 6 bits.
Gray code table
| Number | Binary | Gray |
|---|---|---|
| 0 | 000 | 000 |
| 1 | 001 | 001 |
| 2 | 010 | 011 |
| 3 | 011 | 010 |
| 4 | 100 | 110 |
| 5 | 101 | 111 |
| 6 | 110 | 101 |
| 7 | 111 | 100 |
In Gray code only one bit changes from one number to the next. It is used in rotary encoders and Karnaugh maps. To convert, each Gray bit is the binary bit XOR the bit to its left.
How it works
- Choose the direction and how the input is written.
- Enter the number.
- Read the result in binary, decimal and hex, or check the table.
The conversion
To get the Gray code, XOR the number with itself shifted right by one bit. To go back, XOR the bits down from the top, so each binary bit is the Gray bit XOR the binary bit to its left.
Why it is used
Rotary encoders and position sensors use Gray code because a reading taken in the middle of a change is off by at most one step, not wildly wrong.
Reflected binary
The usual form is called reflected binary code. Each n bit list is the n-1 bit list, then the same list backwards with a 1 in front.
Width
If you enter binary digits, the result keeps the same number of digits. Numbers up to 64 bits are supported.
Questions
Is Gray code the same as the cipher?
No. This is a counting code from engineering, not a secret code. Everything happens in your browser, and nothing you enter is sent anywhere.
Are there other Gray codes?
Yes, but reflected binary is by far the most common, and it is the one used here.
Why does 011 turn into 010?
For 3 (011), shifting right gives 001, and 011 XOR 001 is 010.