Gray Code Visualizer
Gray code is a binary numeral system where two successive values differ in only one bit.
Formula: gray(i) = i ^ (i >> 1)
The red highlighted bits show which bit changed from the previous step. The ▼ marker indicates the position of the changed bit.
Step 1 / 8
| i | Binary | i >> 1 | Gray Code | Dec | Changed Bit |
|---|---|---|---|---|---|
| 0 | 000 | 000 | 000 | 0 | — |
| 1 | 001 | 000 | 001 | 1 | ··▼ |
| 2 | 010 | 001 | 011 | 3 | ·▼· |
| 3 | 011 | 001 | 010 | 2 | ··▼ |
| 4 | 100 | 010 | 110 | 6 | ▼·· |
| 5 | 101 | 010 | 111 | 7 | ··▼ |
| 6 | 110 | 011 | 101 | 5 | ·▼· |
| 7 | 111 | 011 | 100 | 4 | ··▼ |
Calculation for step 0
0 ^ (0 >> 1) = 0 ^ 0 = 0
Binary: 000 (0 in decimal)
Right shift (i >> 1): 000 (0 in decimal)
XOR result: 000 (0 in decimal)
Properties of Gray Code
- Only one bit changes between consecutive values (Hamming distance = 1)
- The sequence is cyclic (last value differs by 1 bit from first)
- Used in error correction, digital communications, and rotary encoders