Search code examples
algorithmgray-code

the nth gray code


the formula for calculating nth gray code is :

(n-1) XOR (floor((n-1)/2))  
(Source: wikipedia)

I encoded it as:

int gray(int n)
{
  n--;
  return n ^ (n >> 1);
}

Can someone explain how the above formula works, or possibly its deriviation?


Solution

  • If you look at binary counting sequence, you note, that neighboring codes differ at several last bits (with no holes), so if you xor them, pattern of several trailing 1's appear. Also, when you shift numbers right, xors also will be shifted right: (A xor B)>>N == A>>N xor B>>N.

        N                    N>>1                  gray
     0000           .        0000           .      0000           .
        | >xor = 0001             >xor = 0000           >xor = 0001
     0001          .         0000          .       0001          .
       || >xor = 0011           | >xor = 0001           >xor = 0010
     0010           .        0001           .      0011           .
        | >xor = 0001             >xor = 0000           >xor = 0001
     0011         .          0001         .        0010         .
      ||| >xor = 0111          || >xor = 0011           >xor = 0100
     0100                    0010                  0110
    

    Original Xor results and shifted results differ in single bit (i marked them by dot above). This means that if you xor them, you'll get pattern with 1 bit set. So,

    (A xor B) xor (A>>1 xor B>>1) == (A xor A>>1) xor (B xor B>>1) == gray (A) xor gray (B)
    

    As xor gives us 1s in differing bits, it proves, what neighbouring codes differ only in single bit, and that's main property of Gray code we want to get.

    So for completeness, whould be proven, that N can be restored from its N ^ (N>>1) value: knowing n'th bit of code we can restore n-1'th bit using xor.

    A_[bit n-1] = A_[bit n] xor gray(A)_[bit n-1]
    

    Starting from largest bit (it is xored with 0) thus we can restore whole number.