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.
Procedure.i gray_encode(n)
  ProcedureReturn n ! (n >> 1)
EndProcedure
 
Procedure.i gray_decode(g)
  Protected bit = 1 << (8 * SizeOf(Integer) - 2)
  Protected b = g & bit, p = b >> 1
 
  While bit > 1
    bit >> 1
    b | (p ! (g & bit))
    p = (b & bit) >> 1
  Wend 
  ProcedureReturn b
EndProcedure
 
If OpenConsole()
  PrintN("Number Gray   Binary  Decoded")
  Define i, n
  For i = 0 To 31
    g = gray_encode(i)
    Print(RSet(Str(i), 2, "0") + Space(5))
    Print(RSet(Bin(g, #PB_Byte), 5, "0") + Space(2))
    n = gray_decode(g)
    Print(RSet(Bin(n, #PB_Byte), 5, "0") + Space(3))
    PrintN(RSet(Str(n), 2, "0"))
  Next
 
  Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
  CloseConsole()
EndIf