In the digital system, only 0 and 1 can be recognized, and all kinds of data can only be processed after being converted into binary code. Gray code is a kind of unweighted code, which adopts absolute coding method. The typical Gray code is a one-step self-complementary code with reflection and cyclic characteristics. Its circulative and one-step characteristics eliminate the possibility of serious errors when taking random numbers, and its reflective and self-complementary characteristics make inversion very convenient. Gray code belongs to reliability coding, which is a coding method with minimum error, because natural binary code can be directly converted into analog signal by digital-to-analog converter, but in some cases, for example, when binary code is converted from decimal 3 to decimal 4, every bit has to be changed, which makes the digital circuit produce a big spike current pulse. Gray code does not have this shortcoming. This is a numerical sorting system, in which all adjacent integers differ in numerical representation by only one number. When it switches between any two adjacent numbers, only one number changes. It greatly reduces the logical confusion from one state to the next. In addition, because there is only one difference between the maximum number and the minimum number, it is usually also called Gray reflection code or cyclic code. The following table is a comparison table of several natural binary codes and Gray codes:
Generally speaking, ordinary binary codes and gray codes can be converted into each other in the following ways:
Binary code->; Gray code (coding): Starting from the rightmost bit, each bit is XOR with the left bit in turn as the value of the corresponding gray code bit, and the leftmost bit remains unchanged (equivalent to 0 on the left);
Gray code-> binary code (decoding): XOR each bit with the left decoded value of the second bit from the left as the decoded value of the bit (the leftmost bit remains unchanged).
Mathematical (computer) description:
Original code: p [0 ~ n]; Gray code: c[0~n](n? n); Code: c = g (p); Decoding: p = f (c); When writing, the numbers decrease from left to right.
Code: c=p XOR p[i+ 1](i? n,0? Me? n- 1),c[n]= p[n];
Decoding: p[n]=c[n], p=c XOR p[i+ 1](i? n,0? Me? n- 1)。
Gray code was proposed by Frank Gray of Bell Laboratories in the 1940s (1880 was proposed by French engineer Jean-Maurice-Emlle).
Baudot), which is used to avoid errors when using PCM(Pusle Code Modulation) method to transmit signals, and obtained the US patent on March 1953. According to the definition, the coding method of Gray code is not unique, but what is discussed here is the most commonly used one.
Gray code was discovered by the ancestors of China people.
Nine chains and gray codes
By analyzing the complete notation of solving nine chains, because only one ring is moved at a time, the expressions of the two steps are only one number apart. Let's take the five rings as an example. The five digits in the first column from the left are in the five-ring state, arranged from the first ring to the fifth ring in turn. The second column is a five-digit number representing the reverse order, which looks like a binary number, but compared with the fourth column, it can be seen that this is not the number of steps represented by a binary number. The third column is the number of steps from the initial state to this state. The rightmost column is the binary representation of the step.
00000-00000-0-00000
10000-0000 1- 1-0000 1
1 1000-000 1 1-2-000 10
0 1000-000 10-3-000 1 1
0 1 100-00 1 10-4-00 100
1 1 100-00 1 1 1-5-00 10 1
10 100-00 10 1-6-00 1 10
00 100-00 100-7-00 1 1 1
00 1 10-0 1 100-8-0 1000
10 1 10-0 1 10 1-9-0 100 1
1 1 1 10-0 1 1 1 1- 10-0 10 10
0 1 1 10-0 1 1 10- 1 1-0 10 1 1
0 10 10-0 10 10- 12-0 1 100
1 10 10-0 10 1 1- 13-0 1 10 1
100 10-0 100 1- 14-0 1 1 10
000 10-0 1000- 15-0 1 1 1 1
000 1 1- 1 1000- 16- 10000
100 1 1- 1 100 1- 17- 1000 1
1 10 1 1- 1 10 1 1- 18- 100 10
0 10 1 1- 1 10 10- 19- 100 1 1
0 1 1 1 1- 1 1 1 10-20- 10 100
1 1 1 1 1- 1 1 1 1 1-2 1- 10 10 1
We found that the number in the right column is the gray code of binary number from decimal number 0 to 2 1! This of course requires 2 1 step. If you write 5-digit binary numbers in turn, it is
10 1 1 1- 1 1 10 1-22- 10 1 10
00 1 1 1- 1 1 100-23- 10 1 1 1
00 10 1- 10 100-24- 1 1000
10 10 1- 10 10 1-25- 1 100 1
1 1 10 1- 10 1 1 1-26- 1 10 10
0 1 10 1- 10 1 10-27- 1 10 1 1
0 100 1- 100 10-28- 1 1 100
1 100 1- 100 1 1-29- 1 1 10 1
1000 1- 1000 1-30- 1 1 1 10
0000 1- 10000-3 1- 1 1 1 1 1
This shows that for a five-link chain with only five links, the state from the initial state to111is not used most, but the state to 000 1 is used most, with 3 1 step. Similarly, for the nine-ring chain, it is not used most from the initial state11111,but in the state 0000000/kloc. Gray code111111stands for binary number 1065438+. This is the mathematical connotation of the nine chains.
Note: from binary number to gray code: check from right to left. If the left side of a number is 0, the number remains unchanged; If it is 1, the number changes (0 becomes 1, 1 becomes 0). For example, the gray code of binary number111is10.
Gray code to binary number: check from right to left, if the sum of the left digits of a number is even, the number remains unchanged; If it is odd, the number will change.
For example, the Gray code1101indicates that the binary number is 100 10.
The above can be used to help memory: 2G becomes zero and G2 becomes parity.
In this way, we can know not only how many steps it takes to use a complete solution from one state to another, but also how many steps it takes to use a simple solution, and what the next action is. (Except for 00000000000 and11111,any state can be transformed into two states, that is, there are two actions. )
For example, suppose that the initial state of nine chains is1110, and the required termination state is 0010165438. How many steps does a simple solution and a complete solution take? What is the first step?
Solution (1) Initial state11kloc-0/10, Gray code 0110/,transformation. The termination status is 00 10011,the gray code is1110065438, and the converted binary number is100. The difference between them is 326-141= 186, and the complete solution needs186 steps.
(2) Since the initial condition of 14 1 is less than the termination condition of 327, the first step should be 142, the corresponding binary is 01000165438, and the conversion to gray code is
(3) Simple solution steps: We calculate the corresponding simple steps from 14 1 327 respectively.
For N= 14 1, we get n0 =103; For N=327, N0=242. The difference between them is 139, so the simple step is 139.