In the code of a group of numbers, if any two adjacent code is different from only one binary number, it is called the Gray Code, in addition, because the maximum number and the minimum number is only one digit difference, namely "end-to-end", so also called cyclic code or reflection code. In a digital system, code is often required to change in a certain order. For example, by increasing the number of natural numbers, if the number of 8421 yards, 0111 to 1000 when the four-bit changes, and in the actual circuit, 4-bit changes can not happen absolutely simultaneously, the count may appear in the short-term other code (1100, 1111, etc.). In certain cases, the circuit status error or input error may be caused. This error can be avoided by using gray code. Gray code is available in a variety of coding formats.
Gray code is available in various formats
decimal Number |
4-bit natural binary code |
4-bit typical gray code |
Decimal Yusangre Code |
decimal Empty six gray code |
decimal Jump six gray code |
Stepper Code |
0 |
0000 |
0000 |
0010 |
0000 |
0000 |
00000 |
1 |
0001 |
0001 |
0110 |
0001 |
0001 |
00001 |
2 |
0010 |
0011 |
0111 |
0011 |
0011 |
00011 |
3 |
0011 |
0010 |
0101 |
0010 |
0010 |
00111 |
4 |
0100 |
0110 |
0100 |
0110 |
0110 |
01111 |
5 |
0101 |
0111 |
1100 |
1110 |
0111 |
11111 |
6 |
0110 |
0101 |
1101 |
1010 |
0101 |
11110 |
7 |
0111 |
0100 |
1111 |
1011 |
0100 |
11100 |
8 |
1000 |
1100 |
1110 |
1001 |
1100 |
11000 |
9 |
1001 |
1101 |
1010 |
1000 |
1000 |
10000 |
10 |
1010 |
1111 |
---- |
---- |
---- |
---- |
11 |
1011 |
1110 |
---- |
---- |
---- |
---- |
12 |
1100 |
1010 |
---- |
---- |
---- |
---- |
13 |
1101 |
1011 |
---- |
---- |
---- |
---- |
14 |
1110 |
1001 |
---- |
---- |
---- |
---- |
15 |
1111 |
1000 |
---- |
---- |
---- |
---- |
The typical gray code in the table is representative. If not specifically stated, Gray code refers to the typical gray code, which can be converted from the natural binary code. Conversion method Recursive generation Code table This method is based on the fact that Gray code is a reflection code, using the following rules of recursion to construct:
- 1-bit gray code has two code words
Li class= "list-num-1-2 list-num-paddingleft-1" > (n+1) bit gray code in the former 2 n code word equals n-bit gray code word, sequentially written, prefixed 0
- (n+1) Bit gray code in the rear 2n code word equals n-bit gray code word, written in reverse order, prefix 1
- n+1 set of Gray code = n-Bit gray code set (sequential) plus prefix 0 + N-bit gray code set (reverse order) plus prefix 1
2-bit gray code |
3-bit gray code |
4-bit gray code |
4-bit natural binary code |
00011110 |
000001011010110111101100 |
0000000100110010011001110101010011001101111111101010101110011000 |
0000000100100011010001010110011110001001101010111100110111101111 |
XOR conversion binary code → Gray Code (Code): This method from the corresponding n-bit binary code word directly obtained n-bit gray code word, the steps are as follows:
- Code word for n-bit binary, from right to left, numbered 0 to N-1
- If the first and i+1 bits of the binary code word are the same, the corresponding gray code of the first bit is 0, otherwise 1 (when i+1=n, the nth bit of the binary word is considered 0, that is, the n-1 bit is unchanged)
The formula means: (G: Gray code, B: binary code)
For example: Binary code 0101, is 4 digits, so its conversion to the Gray code must also be 4 digits, it is advisable to turn into a binary code fifth bit 0, that is, 0 B3 B2 B1 b0. 0 XOR 0=0, so g3=00 xor 1=1, so g2=11 xor 0=1, so g1=10 xor 1=1, so G0=1 converted to a gray code of 0111 |
In fact, from the above Gray code xor or conversion method can be obtained a simple and easy algorithm, for example, for a 2 binary bit not more than 32 bits of the integer x, its gray code can be expressed as (x>>1) ^x.
Algorithm Demo:
#include <stdio.h>#include<stdlib.h>voidPrint_bin (unsignedintValueChar*tail);//print bit bit of a numberintMainintargcChar*argv[]) {unsignedinttemp; for(unsignedintI=0; i<= the; i++) {printf ("%-4d:", i); Temp= (i>>1) ^i;//Conversion code is just one line, very simplePrint_bin (temp,"\ n"); } return 0;}voidPrint_bin (unsignedintValueChar*tail) { for(intI= to; i>=0; i--) {printf ("%d", (Value>>i) &1); } if(tail) {printf ("%s", tail); }}
Compile run Result:
[Email protected]:~/documents/code$GCCTest.c-std=c99-o test.out[email protected]:~/documents/code$./Test.out0:000000000000000000000000000000001:000000000000000000000000000000012:000000000000000000000000000000113:000000000000000000000000000000104:000000000000000000000000000001105:000000000000000000000000000001116:000000000000000000000000000001017:000000000000000000000000000001008:000000000000000000000000000011009:00000000000000000000000000001101Ten:00000000000000000000000000001111 One:00000000000000000000000000001110 A:00000000000000000000000000001010 -:00000000000000000000000000001011 -:00000000000000000000000000001001 the:00000000000000000000000000001000
Algorithm Learning (6) converting----integer to Gray code