Gray Code

DOWNLOAD Mathematica Notebook

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 d_1d_2...d_(n-1)d_n to its corresponding binary reflected Gray code, start at the right with the digit d_n (the nth, or last, digit). If the d_(n-1) is 1, replace d_n by 1-d_n; otherwise, leave it unchanged. Then proceed to d_(n-1). Continue up to the first digit d_1, which is kept the same since d_0 is assumed to be a 0. The resulting number g_1g_2...g_(n-1)g_n is the reflected binary Gray code.

To convert a binary reflected Gray code g_1g_2...g_(n-1)g_n to a binary number, start again with the nth digit, and compute

 Sigma_n=sum_(i=1)^(n-1)g_i (mod 2).

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

 Sigma_(n-1)=sum_(i=1)^(n-2)g_i (mod 2),

and so on. The resulting number d_1d_2...d_(n-1)d_n 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.

Binary plot of the Gray code

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.

00201111040111100
11211111141111101
211221110142111111
310231110043111110
4110241010044111010
5111251010145111011
6101261011146111001
7100271011047111000
81100281001048101000
91101291001149101001
101111301000150101011
111110311000051101010
1210103211000052101110
1310113311000153101111
1410013411001154101101
1510003511001055101100
16110003611011056100100
17110013711011157100101
18110113811010158100111
19110103911010059100110

The binary reflected Gray code is closely related to the solutions of the tower of Hanoi and baguenaudier, as well as to Hamiltonian cycles of hypercube graphs (including direction reversals; Skiena 1990, p. 149).

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.