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.
/*REXX program converts decimal number ───► binary ───► gray code ───► binary.*/
parse arg N .                          /*get the optional argument from the CL*/
if N==''  | N==","   then N=31         /*Not specified?  Then use the default.*/
L=max(1,length(strip(x2b(d2x(N)),'L',0)))   /*find the max binary length of N.*/
w=14                                   /*used for the formatting of cell width*/
_=center('binary',w,'─')               /*the  2nd and 4th  part of the header.*/
say center('decimal', w, "─")'►'     _"►"    center('gray code', w, '─')"►"    _
                                                     /* [+]  the output header*/
     do j=0  to N;     b=right(x2b(d2x(j)),L,0)      /*process   0  ──►  N.   */
     g=b2gray(b)                       /*convert binary number to gray code.  */
     a=gray2b(g)                       /*convert the gray code to binary.     */
     say center(j,w+1)   center(b,w+1)   center(g,w+1)   center(a,w+1)
     end   /*j*/
exit                                   /*stick a fork in it,  we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
b2gray: procedure; parse arg x 1 $ 2;    do b=2  to length(x)
                                         $=$||(substr(x,b-1,1) && substr(x,b,1))
                                         end   /*b*/
        return $
/*────────────────────────────────────────────────────────────────────────────*/
gray2b: procedure; parse arg x 1 $ 2;    do g=2  to length(x)
                                         $=$ || (right($,1)    && substr(x,g,1))
                                         end   /*g*/        /*  ↑  */
                                                            /*  │  */
        return $           /*this is an eXclusive OR  ►─────────┘  */