Generate Gray codes - where consecutive numbers differ by only 1 bit!
🔢 Gray Code Generator
G(i) = i ^ (i >> 1)
Binary (i)
0
Gray Code G(i)
0
📊 Gray Code Sequence
i
Binary
Gray
Changed
📚 Gray Code
Gray code is a binary sequence where consecutive numbers differ by
exactly one bit. This property is useful in error
correction, rotary encoders, and solving Tower of Hanoi.
// Convert binary to Gray code:
Gray(n) = n ^ (n >> 1)
// Example: n = 5 (101)
// n >> 1 = 2 (010)
// 5 ^ 2 = 7 (111)
// The key property:
// G(i) and G(i+1) differ by exactly 1 bit!