A Gray code is an encoding of numbers so that adjacent numbers have a single digit differing by 1. The term Gray code is often used to refer to a "reflected"
code, or more specifically still, the binary reflected Gray code.

To convert a binary number to its corresponding binary reflected Gray
code, start at the right with the digit (the th,
or last, digit). If the is 1, replace by ;
otherwise, leave it unchanged. Then proceed to . Continue up to the first digit , which is kept the same since is assumed to be a 0. The resulting
number is the reflected
binary Gray code.

To convert a binary reflected Gray code to a binary number,
start again with the th
digit, and compute

If is 1, replace by ;
otherwise, leave it the unchanged. Next compute

and so on. The resulting number is the binary
number corresponding to the initial binary reflected Gray code.

The code is called reflected because it can be generated in the following manner. Take the Gray code 0, 1. Write it forwards, then backwards: 0, 1, 1, 0. Then prepend
0s to the first half and 1s to the second half: 00, 01, 11, 10. Continuing, write
00, 01, 11, 10, 10, 11, 01, 00 to obtain: 000, 001, 011, 010, 110, 111, 101, 100,
... (OEIS A014550). Each iteration therefore
doubles the number of codes.

The plots above show the binary representation of the first 255 (top figure) and first 511 (bottom figure) Gray codes. The Gray codes corresponding to the first few nonnegative integers are given in the following table.