Gray code
Karnaugh maps
Create functions to encode a number to and decode a number from Gray code.

Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary).

There are many possible Gray codes. The following encodes what is called "binary reflected Gray code."

Encoding (MSB is bit 0, b is binary, g is Gray code):

if b[i-1] = 1
   g[i] = not b[i]
else
   g[i] = b[i]
Or:

g = b xor (b logically right shifted 1 time)
Decoding (MSB is bit 0, b is binary, g is Gray code):

b[0] = g[0]

for other bits:
b[i] = g[i] xor b[i-1]
Reference
 Converting Between Gray and Binary Codes. It includes step-by-step animations.
go => 
   foreach(I in 0..2**5-1)
      G = gray_encode1(I),
      E = gray_decode1(G),
      printf("%2d %6w %2d %6w %6w %2d\n",I,I.to_binary_string,
                                         G, G.to_binary_string,
                                         E.to_binary_string, E)
   end,
   nl,
   println("Checking 2**1300:"),
   N2=2**1300,
   G2=gray_encode1(N2),
   E2=gray_decode1(G2),
   % println(g2=G2),
   % println(e2=E2),
   println(check=cond(N2==E2,same,not_same)),
   nl.
 
gray_encode1(N) = N ^ (N >> 1).
gray_decode1(N) = P =>
    P = N,
    N := N >> 1,
    while (N != 0)
      P := P ^ N,
      N := N >> 1
    end. 