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 c:\cxpl\codes;          \intrinsic 'code' declarations
 
func Gray2Bin(N);               \Convert N from Gray code to binary
int N;
int S;
[S:= 1;
repeat  N:= N>>S | N;
        S:= S<<1;
until   S=32;
return N;
]; \Gray2Bin
 
 
func Bin2Gray(N);               \Convert N from binary to Gray code
int N;
return N>>1 | N;
 
 
proc BinOut(N);                 \Output N in binary
int N;
int R;
[R:= N&1;
N:= N>>1;
if N then BinOut(N);
ChOut(0, R+^0);
]; \BinOut
 
 
int N, G;
[for N:= 0 to 31 do
        [BinOut(N); ChOut(0, 9\tab\);
        G:= Bin2Gray(N);
        BinOut(G);  ChOut(0, 9\tab\);
        BinOut(Gray2Bin(G));  CrLf(0);
        ];
]