Gray Code Converter (Decimal ⇄ Binary ⇄ Gray Code)
Convert decimal, binary, Gray code, hex and octal in linked boxes — reflected binary where each step changes exactly one bit, computed with XOR.
Working
Gray = Binary XOR (Binary >> 1): each bit is XORed with the bit to its left
| Binary | 1011 |
|---|---|
| Binary >> 1 | 0101 |
| XOR = Gray | 1110 |
XOR truth table
XOR (exclusive or) gives 1 when the two bits differ and 0 when they are the same.
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Convert a list
Gray code table, 0 to 15
| Decimal | Hex | Binary | Gray code |
|---|---|---|---|
| 0 | 0 | 0000 | 0000 |
| 1 | 1 | 0001 | 0001 |
| 2 | 2 | 0010 | 0011 |
| 3 | 3 | 0011 | 0010 |
| 4 | 4 | 0100 | 0110 |
| 5 | 5 | 0101 | 0111 |
| 6 | 6 | 0110 | 0101 |
| 7 | 7 | 0111 | 0100 |
| 8 | 8 | 1000 | 1100 |
| 9 | 9 | 1001 | 1101 |
| 10 | A | 1010 | 1111 |
| 11 | B | 1011 | 1110 |
| 12 | C | 1100 | 1010 |
| 13 | D | 1101 | 1011 |
| 14 | E | 1110 | 1001 |
| 15 | F | 1111 | 1000 |
What Gray code is
Gray code, also called reflected binary code, is an ordering of binary numbers in which any two successive values differ in exactly one bit. That single-bit-change property makes it valuable for rotary encoders, position sensors and state machines, where a glitchy multi-bit transition in ordinary binary could momentarily read a completely wrong value. Converting binary to Gray code is a simple bitwise operation: the first bit stays the same, and every other Gray bit is the XOR of the current binary bit with the one before it, which is the same as computing b XOR (b shifted right by one).
Using this converter
Type a decimal number, a binary string, a Gray code, or a hexadecimal or octal number and the other boxes update live — so you can go decimal → Gray directly (the way encoder positions and textbook exercises are framed) or decode a Gray value straight back to a plain number. Binary to Gray applies g[i] = b[i-1] XOR b[i] per position, and Gray to binary reverses it with a cumulative XOR, so b[i] = b[i-1] XOR g[i]. The binary and Gray boxes accept only the digits 0 and 1, and any other character shows a clear message. Leading zeros are preserved, so an 8-bit pattern stays 8 bits wide.
Frequently asked questions
How do I convert binary to Gray code by hand?
Keep the most significant bit as-is, then XOR each remaining binary bit with the bit to its left. For example 1011 becomes 1110: the leading 1 stays, then 1⊕0=1, 0⊕1=1 and 1⊕1=0. This tool does exactly that for you as you type.
How do I convert Gray code back to binary?
The first bit is copied unchanged, then each following binary bit is the XOR of the previous binary bit with the current Gray bit — a running cumulative XOR. So 1110 decodes back to 1011. Just type into the Gray code box and the binary result appears instantly.
Can I see how the conversion works, or convert a whole list?
Yes. A working table shows the shift and the XOR for your own number, a list box converts one value per line in either direction, and a reference table lays the codes out side by side. The list box can also open a .txt, .csv or .log file and download its converted column, and an “XOR truth table” under the working shows A XOR B for every pair of bits — 1 when they differ.
Can I type hex or octal, or send a conversion as a link?
Yes. Hexadecimal and Octal boxes sit after the Gray code box and work in both directions like the others, with or without a 0x or 0o prefix; a digit the box cannot read is refused, naming what it takes. Copy link carries the number, the box it was typed in and the bit width, so the link opens the same conversion.
Can it Gray-code a piece of text, byte by byte?
Not as text, on purpose. Gray code is made for counters and position encoders, where each step changes a single bit; applied to the bytes of a message it is only a reversible scramble that hides nothing. If you want it anyway, turn the text into binary bytes with the Text to Binary tool, put one byte on each line in Convert a list here, and choose Binary → Gray.