#math/discrete #cs/cp
## Summary
A Gray code is an ordering of binary values in which adjacent codewords differ by exactly one bit. It is useful in hardware, communications, and ML [[genetic algorithms]] because it provides smooth transitions between states. For example, if a binary encoder is between $3\,(011)$ and $4\,(100)$, several bits may change at slightly different times and briefly produce an unrelated value such as $7\,(111)$. With Gray code, only one bit changes, so a transitional reading is either the previous codeword or the next one rather than an unrelated value.
Let $G(i)$ represent the $i^{\text{th}}$ codeword in the Gray-code sequence. For example, the first three codewords have decimal values $0 \to 1 \to 3$:
$
(00)_{2} \to (01)_{2} \to (11)_{2}
$
## The Pattern
For $G(n)$, the $i^{\text{th}}$ bit equals $1$ exactly when the adjacent bits $b_{i+1}b_i$ of $n$ are $01$ or $10$. It equals $0$ when they are $00$ or $11$.
For example:
$
\begin{align}
n &= (110)_{2} \\
G(n) & = (101)_{2} \\
\end{align}
$
This gives the following formula for the $n^{\text{th}}$ Gray codeword, $G(n)$:
$
G(n) = n \oplus (n \gg 1 )
$
### Gray Code: The Effect of Adding 1
Gray code guarantees that $G(n)$ and $G(n+1)$ differ by exactly one bit. When we add $1$ to $n$, the binary addition behaves in one of two ways.
**Case 1: No Carry**
If $n$ is even, its least significant bit (LSB) is $0$. Adding $1$ flips this bit to $1$ without affecting higher bits. The LSB of $(n \gg 1)$ is unchanged, so only the LSB of the resulting Gray code flips.
$\begin{align*} G(6) &= 110 \oplus 011 = 101 \\ G(7) &= 111 \oplus 011 = 100 \quad \text{(Only the LSB changed)} \end{align*}$
**Case 2: Carry**
If $n$ ends in $i$ trailing ones preceded by a zero (the $(i+1)^{\text{th}}$ bit from the right), adding $1$ flips those ones to zeros and the preceding zero to one. In other words, $0$ followed by $i$ ones becomes $1$ followed by $i$ zeros. After the XOR, the $i$ least significant bits are unchanged.
$(i=2) \qquad \begin{align*} G(3) &= 011 \oplus 001 = 010 \\ G(4) &= 100 \oplus 010 = 110 \quad \text{(Only the 3rd bit changed)} \end{align*}$
**The $(i + 1)^{\text{th}}$ Bit Flips**
The $(i + 1)^{\text{th}}$ bit of $n$ goes from $0$ to $1$ when we add $1$.
- If the $(i+2)^{\text{th}}$ bit was $1$, then the XOR for the $(i+1)^{\text{th}}$ bit goes from $1$ to $0$.
- If the $(i+2)^{\text{th}}$ bit was $0$, then the XOR for the $(i+1)^{\text{th}}$ bit goes from $0$ to $1$.
- $a \oplus b \neq \neg a \oplus b$ where $a,b \in \{0,1\}.$
**The $(i+2)^{\text{th}}$ Bit and Beyond are Preserved**
Adding $1$ does not affect anything beyond the $(i+1)^{\text{th}}$ bit, so all higher XOR results stay the same.
> [!Insight]
> Case 1 is just case 2 where $i=0$.
## Code
```cpp
unsigned int gray(unsigned int n) {
return n ^ (n >> 1);
}
```
## Finding the Inverse
Let $n_k$ be the $k^{\text{th}}$ bit of an integer $n$. Then,
$
\begin{align}
n_{k} &= G(n)_{k} \\
n_{k-1} &= G(n)_{k-1}\oplus n_{k} = G(n)_{k} \oplus G(n)_{k-1}\\
n_{k-2} &= G(n)_{k} \oplus G(n)_{k-1} \oplus G(n)_{k-2}\\
\dots \\
n_{1} &= G(n)_{k} \oplus G(n)_{k-1} \oplus \dots \oplus G(n)_{1}\\
\end{align}
$
```cpp
// Here, g = G(n).
unsigned int reverse_gray(unsigned int g) {
unsigned int n = 0;
for (; g; g >>= 1) {
// XOR is 1 when the bits differ and 0 when they match.
n ^= g;
}
return n;
}
```