⚙️ Digital Logic & Rotary Encoders
Binary to Gray Code & Gray to Binary Converter
Convert between standard natural binary numbers and $\text{Reflected Binary Gray Code}$ ($G = B \oplus (B \gg 1)$) with bitwise XOR explanations and optical encoder angle mapping.
🔢 Decimal 7 (Bin 0111 → Gray 0100)
🔢 Decimal 15 (Bin 1111 → Gray 1000)
🔢 8-bit Max (255 → Gray 10000000)
🤖 Decimal 42 (Bin 101010)
Input Number
Reflected Gray Code
1111
Natural Binary
1010
Gray Code
1111
Decimal Value
10
💡 Bitwise Gray Code Formula:
$$G = B \oplus (B \gg 1) \implies \text{Gray Code } 1111_2 \text{ for Binary } 1010_2$$
4-Bit Standard Binary vs Reflected Gray Code Truth Table:
| Decimal | Natural Binary | Gray Code | Rotary Encoder Angle (360°/16) |
|---|
Why Gray Code is Vital for Optical Encoders
The Gray code (Reflected Binary Code, invented by Frank Gray at Bell Labs in 1953) has the unique property that only one single bit changes between any two consecutive values:
- Rotary Encoder Glitch Prevention: In natural binary, transitioning from $3$ (`011`) to $4$ (`100`) requires 3 bits to flip simultaneously. Physical manufacturing tolerances can cause mechanical switches to briefly output false intermediate states (e.g. `111` or $7$). In Gray code, only 1 bit flips (`010` → `110`), eliminating digital glitching.
- Conversion Formulas:
- Binary → Gray: $G = B \oplus (B \gg 1)$
- Gray → Binary: Most significant bit $B_0 = G_0$; each subsequent bit $B_i = B_{i-1} \oplus G_i$.