Gray code is a binary numeral system in which the binary representations of any two adjacent numbers differ in exactly one bit. For example, the Gray code sequence for -bit binary numbers is
Note that sequence indices start at ; that is, .
Gray code was proposed by Frank Gray of Bell Labs in the 1940s and patented in 1953.
Constructing Gray code (transformation)
There are many ways to construct Gray code. We first introduce a manual construction, then give code and a correctness proof.
Manual construction
A -bit Gray code can be constructed as follows. Start with the all- Gray code and use these rules:
- Flip the least significant bit to obtain the next Gray code (for example, );
- Flip the bit to the left of the rightmost to obtain the next Gray code (for example, ).
Applying these rules alternately for steps produces the sequence of -bit Gray codes.
Mirror construction
A -bit Gray code can be obtained quickly from a -bit Gray code by mirroring it vertically and then prefixing a new bit, as shown below:
Computing Gray code
Let us compare the binary representations of and . The binary representation of has bit equal to exactly when 's bit is and bit is , or bit is and bit is . Thus, this is an XOR operation:
int g(int n) { return n ^ (n >> 1); }
Correctness proof
We now prove that in the Gray code sequence generated by the formula above, the binary representations of every pair of adjacent Gray codes differ in exactly one bit.
Consider the difference between and . Consider the operation on of adding . This flips all of 's consecutive bits at the end to zero, then changes the lowest to . Write the binary representations of and as follows:
When computing and , the last bits both become the form , while bit is different. This is because and are identical except for their last bits, so bit is XORed with either in both cases or in both cases. In either case, bit is different. Apart from the last bits, the other bits are XORed in the same way, so their results are identical.
This proves the claim.
Constructing the original number from Gray code (inverse transformation)
Next, consider the inverse transformation of Gray code: given a Gray code , find the original number . Traverse the binary bits from the most significant bit to the least significant bit (the least significant bit has index , namely the units bit; the most significant bit has index ). The relationship between 's binary bit and 's binary bit , denoted , is:
int rev_g(int g) {
int n = 0;
for (; g; g >>= 1) n ^= g;
return n;
}
Applications
Gray code has several useful applications, some of which are unexpected:
-
The Gray code sequence of -bit binary numbers can be viewed as a Hamiltonian cycle through the vertices of a -dimensional hypercube (a square in two dimensions and a unit segment in one dimension), where each bit of the Gray code represents one coordinate dimension.
-
Gray code is used to minimize errors in signal transmission in digital-to-analog converters (such as sensors), because only one bit changes at a time.
-
Gray code can be used to solve the Tower of Hanoi problem.
Let the number of disks be . Start from the -bit all- Gray code and move to the next Gray code in sequence (move from to ). The -th binary bit of the current Gray code represents the -th disk, counting from smallest to largest.
Since exactly one binary bit changes at each step, move disk whenever bit changes. During the disk moves, every disk other than the smallest has only one possible destination. When moving the first disk, there are always two possible destinations. Thus, use the following strategy:
If is odd, the disk-movement path is , where is the starting peg, is the peg to which all disks are ultimately moved, and is the remaining peg.
If is even:
-
Gray code is also used in the theory of genetic algorithms.
Exercises
-
CSP S2 2019 D1T1 Difficulty: easy
-
SGU #249 Matrix Difficulty: medium
Some content on this page is translated from the blog post Код Грея and its English translation Gray code. The Russian version is licensed under Public Domain + Leave a Link; the English version is licensed under CC-BY-SA 4.0.