Gray Code (grey code)
Typical binary grey code (binary Gray Code) is abbreviated as gray code. Originally for communication, it is now commonly used in analog-to-digital conversions and position-to-digital conversions.
The characteristic is: in a group of numbers of code, if any two adjacent codes have only one binary number different, then call this code as gray code.
- Gray code belongs to the reliability coding, which is a kind of error minimization coding method.
- Gray code is an absolute encoding method.
- Because gray code is a variable weight code.
- The parity of the decimal number of gray code is the same as the parity of the number of 1 in the code word.
Convert Decimal to Gray code
Well, we've already covered that much, so how do I convert a decimal number into a gray code?
- First, the decimal number is converted into binary form.
- For n-bit binary code words, from right to left, numbered 0 to n-1.
- If the first bit and i+1 bit 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).
For example:
First binary: 1100
Then the binary code is:1100
Number is0-3
And then in1100
Pre-fill One0
, it becomes01100
To start the operation.
0
XOR or1
to 1.
1
XOR or1
to 0.
1
XOR or0
to 1.
0
XOR or0
to 0.
So Gray code for1010
。
Convert Gray code to decimal
Now let's suppose to get to a gray code for 1010
. How to decode to decimal. From the second position on the left, the decoded value of each bit and the left one is the same as the decoded value of the bit (the leftmost one remains unchanged). XOR, in turn, until the lowest bit. The value of the binary code after the conversion (binary number) is the value of the gray code.
So the result of the operation is:
0
With the first bit1
Make an XOR or result of 1
Above results1
With the second digit0
XOR result is 1
Above results1
and third place1
XOR result is 0
Above results0
With fourth digit0
XOR result is 0
So the binary value is:1100
---Decimal:12
.
Code implementation
Put the code implementation below, this code only put the coding part, not put decoding. If there is a problem with decoding, you can comment to tell me.
main.cpp//graycode_leetcode////Created by Alps on 14/12/7.//Copyright (c) 2014 Chen. All rights reserved.//#include <iostream> #include <vector> #include <math.h> #include <string.h >using namespace Std;class solution{public:vector<int> graycode (int n) {vector<int> gray; if (n < 1) {gray.push_back (0); return gray; } int num = POW (2,n); int graycode[n]; for (int i = 0; i < num; i++) {inttobit (Graycode, I, N); Bittogray (Graycode,n); Gray.push_back (Graybittoint (Graycode, N)); } return gray; } void Inttobit (int *code, int n, int bit) {int i = bit-1; while (I >= 0) {code[i--] = n%2; n/=2; }} void Bittogray (int *code, int bit) {int temp[bit]; Temp[0] = 0^code[0]; for (int i = 0; i < bit-1; i++) {temp[i+1] = code[i]^code[i+1]; } for (int i = 0; i < bit; i++) {code[i] = Temp[i]; }} int Graybittoint (int *code, int bit) {int number = 0; for (int i = 0; i < bit; i++) {if (code[i] = = 1) {number + = POW (2, bit-i-1); }} return number; }};int Main (int argc, const char * argv[]) {vector<int> test; Solution SL; Test = Sl.graycode (3); Vector<int>::iterator ITER; for (iter = Test.begin (); ITER! = Test.end (); iter++) {printf ("%d\n", *iter); } return 0;}
Algorithmic learning-Interpretation of gray Code (grey code) and C + + implementation