Gray Code Algorithm

The reflected binary code (RBC), also known exactly as reflected binary (RB) or Gray code after Frank Gray, is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit).Gray codes are widely used to prevent spurious output from electromechanical switches and to facilitate mistake correction in digital communications such as digital terrestrial television and some cable television systems.

Gray Code source code, pseudocode and analysis