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.
# Tested using GAWK
 
function bits2str(bits,        data, mask)
{
    # Source: https://www.gnu.org/software/gawk/manual/html_node/Bitwise-Functions.html
    if (bits == 0)
        return "0"
 
    mask = 1
    for (; bits != 0; bits = rshift(bits, 1))
        data = (and(bits, mask) ? "1" : "0") data
 
    while ((length(data) % 8) != 0)
        data = "0" data
 
    return data
}
 
function gray_encode(n){
    # Source: https://en.wikipedia.org/wiki/Gray_code#Converting_to_and_from_Gray_code
    return xor(n,rshift(n,1))
}
 
function gray_decode(n){
    # Source: https://en.wikipedia.org/wiki/Gray_code#Converting_to_and_from_Gray_code
    mask = rshift(n,1)
    while(mask != 0){
        n = xor(n,mask)
        mask = rshift(mask,1)
    }
    return n
}
 
BEGIN{
    for (i=0; i < 32; i++)
        printf "%-3s => %05d => %05d => %05d\n",i, bits2str(i),bits2str(gray_encode(i)), bits2str(gray_decode(gray_encode(i)))
}