Gray code is a binary number system in which only one bit of two adjacent numbers differs. For example, the Gray code sequence of a 3 bit binary numbers is:
000,001,011,010,110,111,101,100
Note that we use 0 as the starting point for the subscript of the sequence, that is, G(0)=000,G(4)=110 .
Gray code was discovered in 1940s and patented in 1953 by Frank Gray of Bell Laboratories.
Let's look at the binary of n and G(n) . It can be found that if the binary position i-th bit of G(n) is 1 , only when the binary i-th bit of n is 1 and the i+1-th bit is 0 or the i-th bit is 0 , and the i+1-th bit is 1 . So we can treat it as an XOR operation, that is:
Next, let's prove that in the Gray code sequence generated according to the above formula, there exists and only exists one different bit of two adjacent Gray code.
We consider the difference between n and n+1 . Adding n by 1 is equivalent to turning all consecutive 1 at the end of the binary n into inversions, and then changing the lowest bit of 0 to 1 . We represent the binary bits of n and n+1 like this:
So when we calculate g(n) and g(n+1) , the next k bits will become the form like \displaystyle\underbrace{100\cdots00}_{k\text{entries}} , and the k+1-th bit is different, because n and n+1 are the same except for the last k+1 bits. Therefore, the position k+1 is either XORed 1 or 0 at the same time. In both cases, the k+1-th bit is different. The binary bits except the last k+1 bits are also subjected to the same XOR operation, and the result is the same.
Q.E.D.
Construct the original number through Gray code (inverse transformation)¶
Next, we consider the inverse transformation of the Gray code, that is, given a Gray code g and ask you to find the original number n . We consider traversing from the highest bit of the binary to the lowest bit (the subscript of the lowest bit is 1 , which is the one bit; and the highest bit subscript is k ). Then the relationship between the i-th bit of n and the i-th bit of g is shown as follows:
Gray code has some very useful applications, some of which are unexpected:
The Gray code sequence of a k bit binary number can be regarded as a Hamiltonian loop of the vertices of a hypercube (a square in two dimensions, a unit vector in one dimension) in the k dimensional space, where each bit of the Gray code represents one dimension of coordinates.
Gray code is used to minimize errors in the signal transmission of digital-to-analog converters (such as sensors) because it only changes one bit at a time.
Gray code can be used to solve the problem of the Tower of Hanoi.
Assume the number of disks as n . We start from the Gray code G(0) with all 0 in n and move to the next Gray code in turn ( G(i) moves to G(i+1) ). The i-th bit of the current Gray code represents the i-th plate from small to large.
Since only one binary bit changes each time, when the i-th bit changes, we move the i-th plate. While moving the plates, except for the smallest one, there can only be one placement option for any other plate when it is moving. When moving the first plate, we always have two placement options. So our strategy is as follows:
If n is an odd number: The moving path of the plate is f\to t\to r\to f\to t\to r\to\cdots , where f is the first pillar, t is the pillar where we put all the plates in the end, and r is the pillar in the middle.
If n is an even number: f \to r \to t \to f \to r \to t \to \cdots
Gray code is also applied in genetic algorithm theory.
Part of this page is translated from the blog post Код Грея and its English version Gray code. The copyright license for the Russian version is Public Domain + Leave a Link; And the copyright license for the English version is CC-BY-SA 4.0.
buildLast update and/or translate time of this article,Check the history editFound smelly bugs? Translation outdated? Wanna contribute with us? Edit this Page on Github peopleContributor of this article sshwy translateTranslator of this article Visit the original article! copyrightThe article is available under CC BY-SA 4.0 & SATA ; additional terms may apply.