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.
Conversion method
Recursive generation Code tableThis 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 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) post 2 in gray code n code word equals n bit Gray code word, in reverse order, plus prefix 1
xor or conversion
Binary code → Gray Code (code):
binary code → Gray code (decoding):
Program implementation
1. Xor or conversion
1#include <iostream>2#include <vector>3#include <string>4#include <cmath>5#include <math.h>6 using namespacestd;7 //decimal Turn binary/8 //intput: decimal number, and required number of digits9 //output: binary stringTen stringNum2binary (intNumintbitnum); One A //Implement n-bit gray code - //intput: Number of bits in gray code - //Output:graycode the voidGetgraycode (intbitnum); - voidGetGrayCode2 (intbitnum,vector<string> &graycode);//Real -World arguments are changed by reference - - intMain () + { - intm= *; + stringC=num2binary (M,8); Acout<<c<<Endl; atvector<string>rel; - /*Getgraycode (4);*/ -GetGrayCode2 (4, rel); - for(vector<string>::size_type i=0; I!=rel.size (); i++) - { -cout<<rel[i]<<Endl; in } - return 0; to } + - voidGetgraycode (intbitnum) the { * stringGraycode; $ for(intI=0; I<pow (2, bitnum); i++)Panax Notoginseng { - intTemp= (i>>1)^i; theGraycode=(Num2binary (Temp,bitnum)); +cout<<graycode<<Endl; A } the } + - voidGetGrayCode2 (intbitnum,vector<string> &Graycode) $ { $ for(intI=0; I<pow (2, bitnum); i++) - { - intTemp= (i>>1)^i; the stringStr=num2binary (temp,bitnum); - graycode.push_back (str);Wuyi /*cout<<graycode[i]<<endl;*/ the } - } Wu stringNum2binary (intNumintbitnum) - { About stringstr; $ stringresult=""; - for(inti=bitnum-1; i>=0; i--) - { - intX= (num>>i) &1; A StringStream SS; +ss<<x; theSs>>str; -result+=str; $ /*str.clear ();*/ the } the returnresult; the}
View Code
2. Recursive Code table implementation
1vector<string> GetGrayCode3 (intbitnum)2 {3vector<string> Graycode (POW (2, Bitnum));4 if(bitnum==1)5 {6graycode[0]="0";7graycode[1]="1";8 returnGraycode;9 }Ten Else One { Avector<string> Lastgraycode=getgraycode3 (bitnum-1); - for(vector<string>::size_type i=0; I!=lastgraycode.size (); i++) - { thegraycode[i]="0"+Lastgraycode[i]; -Graycode[graycode.size ()-1-i]="1"+Lastgraycode[i]; - } - for(vector<string>::size_type j=0; J!=graycode.size (); j + +) + { -cout<<graycode[j]<<Endl; + } A returnGraycode; at } - -}
View Code
Gray Code implementation