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.
$ include "seed7_05.s7i";
  include "bin32.s7i";
 
const func integer: grayEncode (in integer: n) is
  return ord(bin32(n) >< bin32(n >> 1));
 
const func integer: grayDecode (in var integer: n) is func
  result
    var integer: decoded is 0;
  begin
    decoded := n;
    while n > 1 do
      n >>:= 1;
      decoded := ord(bin32(decoded) >< bin32(n));
    end while;
  end func;
 
const proc: main is func
  local
    var integer: i is 0;
  begin
    for i range 0 to 32 do
      writeln(i <& " => " <& grayEncode(i) radix 2 lpad0 6 <& " => " <& grayDecode(grayEncode(i)));
    end for;
  end func;