When to use it
In Gray code, consecutive values differ in exactly one bit. That's gold for crossing clock domains: if you sample a Gray-coded counter mid-transition you can only ever read the old or the new value, never a garbage in-between, so it's how async FIFO pointers are carried across.
How it works
Binary to Gray is a single row of XORs: g = b ^ (b >> 1). Gray back to binary
is a running (prefix) XOR from the top bit down. Both are cheap, unclocked logic.
Gotchas
- Gray code is for counting (adjacent values). Don't expect arbitrary jumps to differ by one bit, only consecutive ones do.
- Convert to binary before you do arithmetic on it; Gray values don't add.