Gray code, also known as cyclic binary code or reflection binary code, Gray code is a kind of coding method that we often encounter in engineering. Its basic feature is that any two adjacent codes have only one binary number different. The point will be explained in detail below. The basic feature of Gray code is that any two adjacent codes have only one binary number, which is important. The conversion relationship between commonly used binary numbers and Gray code is as follows:
Second, the exchange of binary Gray code and natural binary code1, binary code converted to binary Gray code
The binary code is converted into a binary Gray code. The rule is to reserve the highest bit of the binary code as the highest bit of the Gray code, and the second highest bit Gray code is the difference between the upper and the second highest bits of the binary code, and the rest of the Gray code and the next highest bit The method is similar.
Conversion code:
/ / According to the law of binary conversion to Gray code, you can get the following code:
Static unsigned int DecimaltoGray(unsigned int x)
{
Return x^(x》》1);
}
/ / The above code realizes the conversion of unsigned int type data to Gray code, the highest convertible 32-bit natural binary code, beyond 32 bits will overflow.
Static int DecimaltoGray( int x)
{
Return x^(x》》1);
}
/ / The above code to achieve the conversion of int data to Gray code, the highest convertible 31-bit natural binary code, beyond 31 will overflow. 12345678910111213
2, binary Gray code is converted into binary code
The binary Gray code is converted into a binary code, the law is to reserve the highest bit of the Gray code as the highest bit of the natural binary code, and the second highest natural binary code is the high bit natural binary code and the next highest bit Gray code is different, and the natural binary code The rest of the people are similar to the second highest natural binary code.
Conversion code:
According to the law of converting binary Gray code into natural binary code, the following three code modes can be obtained:
ï‚· static unsigned int GraytoDecimal(unsigned int x)
ï‚· {
ï‚· unsigned int y = x;
 while(x》"=1)
ï‚· y ^= x;
ï‚· return y;
ï‚· }
ï‚· static unsigned int GraytoDecimal(unsigned int x)
ï‚· {
 x^=x》"16;
 x^=x》"8;
 x^=x》"4;
 x^=X》》2;
ï‚· x^=x^1;
ï‚· return x;
ï‚· }
ï‚· static unsigned int GraytoDecimal(unsigned int x)
ï‚· {
ï‚· int i;
ï‚· for(i=0;(1"i"sizeof(x)*8;i++)
ï‚· {
 x^=x》†(1 “i);
ï‚· }
ï‚· return x;
ï‚· }
/ / The above code realizes the conversion of unsigned int type data to natural binary code, the highest convertible 32-bit Gray code, beyond 32 bits will overflow. Change the data type to I
Network Accessories,Wifi Adapter,Fiber Optic Network Components,Splitter Fiber Optic
Cixi Dani Plastic Products Co.,Ltd , https://www.dani-fiber-optic.com