Post · 3 min read· pp. 98–99

Gray code

Gray code is a binary numeral system where two successive values differ in only one bit.

Denote by G(n)G(n) the representation of the number nn using the Gray code. The sequence of Gray codes for 3-bit numbers is: 000, 001, 011, 010, 110, 111, 101, 100, so G(4)=(110)2=6G(4) = (110)_2 = 6. For example, G(3)=(010)2G(3) = (010)_2 and G(4)=(110)2G(4) = (110)_2 differ at exactly one bit, the leftmost bit. Similarly, G(4)=(110)2G(4) = (110)_2 and G(5)=(111)2G(5) = (111)_2 differ at exactly one bit, the rightmost one. This is true for all successive numbers.

This code was invented by Frank Gray in 1953.

Finding Gray code

Let's look at the bits of number nn and the bits of number G(n)G(n). Notice that ii-th bit of G(n)G(n) equals 1 only when ii-th bit of nn equals 1 and i+1i + 1-th bit equals 0 or the other way around (ii-th bit equals 0 and i+1i + 1-th bit equals 1). Thus, G(n)=n⊕(n>>1)G(n) = n \oplus (n >> 1):

text
int g (int n) {
    return n ^ (n >> 1);
}

Finding inverse Gray code

Given Gray code gg, restore the original number nn.

We will move from the most significant bits to the least significant ones (the least significant bit has index 1 and the most significant bit has index kk). The relation between the bits nin_i of number nn and the bits gig_i of number gg:

nk=gk,nk−1=gk−1⊕nk=gk⊕gk−1,nk−2=gk−2⊕nk−1=gk⊕gk−1⊕gk−2,nk−3=gk−3⊕nk−2=gk⊕gk−1⊕gk−2⊕gk−3,⋮\begin{align} n_k &= g_k, \\ n_{k-1} &= g_{k-1} \oplus n_k = g_k \oplus g_{k-1}, \\ n_{k-2} &= g_{k-2} \oplus n_{k-1} = g_k \oplus g_{k-1} \oplus g_{k-2}, \\ n_{k-3} &= g_{k-3} \oplus n_{k-2} = g_k \oplus g_{k-1} \oplus g_{k-2} \oplus g_{k-3}, \vdots \end{align}

The easiest way to write it in code is:

text
int rev_g (int g) {
  int n = 0;
  for (; g; g >>= 1)
    n ^= g;
  return n;
}

Practical applications

Gray codes have some useful applications, sometimes quite unexpected:

  • Gray code of nn bits forms a Hamiltonian cycle on a hypercube, where each bit corresponds to one dimension.
  • Gray codes are used to minimize the errors in digital-to-analog signals conversion (for example, in sensors).
  • Gray code can be used to solve the Towers of Hanoi problem. Let nn denote number of disks. Start with Gray code of length nn which consists of all zeroes (G(0)G(0)) and move between consecutive Gray codes (from G(i)G(i) to G(i+1)G(i+1)). Let ii-th bit of current Gray code represent ii-th disk (the least significant bit corresponds to the smallest disk and the most significant bit to the biggest disk). Since exactly one bit changes on each step, we can treat changing ii-th bit as moving ii-th disk. Notice that there is exactly one move option for each disk (except the smallest one) on each step (except start and finish positions). There are always two move options for the smallest disk but there is a strategy which will always lead to answer: if nn is odd then sequence of the smallest disk moves looks like f→t→r→f→t→r→...f \to t \to r \to f \to t \to r \to ... where ff is the initial rod, tt is the terminal rod and rr is the remaining rod, and if nn is even: f→r→t→f→r→t→...f \to r \to t \to f \to r \to t \to ....
  • Gray codes are also used in genetic algorithms theory.

Practice Problems