Decimal to Binary Converter
Decimal numbers to binary, with two's complement.
This binary to Gray code converter turns a binary number into its reflected Gray code, or decodes Gray code back to binary. Each bit of the working is shown, so you can follow the XOR steps.
gray = binary XOR (binary >> 1)
The first (leftmost) bit stays the same. Every other Gray bit is the XOR of a binary bit and the bit to its left.
To go back, keep the first bit, then XOR each Gray bit with the binary bit you just produced:
binary[i] = binary[i − 1] XOR gray[i]
| Decimal | Binary | Gray code |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Each row differs from the next in exactly one bit, including the wrap from 15 back to 0.
In Gray code, two neighbouring numbers differ in only one bit. That matters when a value is read by a sensor. A rotary encoder moving from 7 (0111) to 8 (1000) in plain binary flips four bits at once, and if they don't all change at the same instant the reading can jump to any value. In Gray code the same step flips one bit, so there is no false reading. Gray code is also used in Karnaugh maps and in some error-correction schemes.
Keep the first bit, then write the XOR of each pair of neighbouring bits. 1010 becomes 1, 1⊕0 = 1, 0⊕1 = 1, 1⊕0 = 1: 1111.
Keep the first bit, then XOR each Gray bit with the previous binary bit.
The sequence for n bits is built by writing the (n−1)-bit sequence, then the same list in reverse (reflected), with a 0 in front of the first half and a 1 in front of the second.
Often used together with the Binary to Gray Code Converter.
Decimal numbers to binary, with two's complement.
Any base from 2 to 36, with binary, octal, decimal, and hex.
Text to 8-bit binary and binary to text.