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.
 
Module Code32  (&code(), &decode()){
      Const d$="{0::-2} {1:-6} {2:-6} {3:-6} {4::-2}"
      For i=0 to 32
            g=code(i)
            b=decode(g)
            Print format$(d$, i, @bin$(i), @bin$(g), @bin$(b), b)
      Next
      // static function
      Function bin$(a)
            a$=""
            Do n= a mod 2 : a$=if$(n=1->"1", "0")+a$ : a|div 2 : Until a==0
            =a$
      End Function
}
Module GrayCode {
      Module doit (&a(), &b()) { }
      Function GrayEncode(a) {
            =binary.xor(a, binary.shift(a,-1))
      }
      Function GrayDecode(a) {
            b=0
            Do b=binary.xor(a, b) : a=binary.shift(a,-1) : Until a==0
            =b
      }
      // pass 2 functions to Code32
      doit &GrayEncode(), &GrayDecode()
}
// pass Code32 to GrayCode in place of doit
GrayCode ; doit as  Code32
 