In the code of a group of numbers, if any two adjacent code is different from only one binary number, it is said that this code is gray code (gray code), gray code is composed of 0 and 1, the binary code evolved.
The Gray code generation method is as follows:
- 1-bit gray code with two code words
- (n+1) Bit gray code in the first 2n code word equal to n-bit gray code word, in order to write, prefixed by 0
- (n+1) Bit gray code in the following 2n code word equals n-bit gray code word, in reverse order, plus prefix 1
Examples are as follows:
1-bit Gray code:
0
1
2-bit Gray code:
A total of 2^2 code word, the first half of the code word for 1-bit Gray code in order to write the prefix 0, the second half of the code word for 1-bit gray code reverse writing prefix 1, the results are as follows:
00
01
11
10
3-bit gray code is:
Based on the 2-bit gray code, the results are as follows:
000
20s
011
010
110
111
101
100
In the same vein, n-bit gray code can be obtained.
Binary conversion into Gray code:
The principle is that the highest bit of the binary code is retained as the highest bit of the Gray code, the second highest bit of the Gray code is the binary coded maximum and the sub-high difference or, the other bits of the gray code and the second high of the method similar.
Binary code:1011 to be converted to gray Code
1011 = 1 (according to the first digit), 1 (First and second XOR 1^0 = 1), 1 (second or third digit, 0^1=1), 0 (1^1 =0) = 1110
Actually equals (1011 >> 1) ^ 1011 = 1110
Gray code and binary code converted into gray code