Algorithm learning-Gray Code (Gray Code) Interpretation and c ++ implementation
Gray Code (Gray Code)
A typical Binary Gray Code is short for Gray Code. Originally for communication, it is now commonly used in analog-digital conversion and location-digital conversion.
This encoding is called Gray code if any two adjacent codes have only one binary number.
Gray code is a reliable encoding method that minimizes errors. Gray code is an absolute encoding method. Gray code is a type of authorization code. The decimal parity of the gray code is the same as that of the number 1 in the codeword. Convert decimal to Gray Code
Okay, we have already introduced so much. How can I convert a decimal number into a gray code?
First, convert the decimal number to the binary format. For the n-bit binary code word, from right to left, 0 to n-1 number. If the I-bit and I + 1 of the binary code are the same, the I-bit of the gray code is 0; otherwise, it is 1 (when I + 1 = n, the Nth bit of the binary code is regarded as 0, that is, the nth bit of the binary code remains unchanged ).
For example:
First 12-> binary: 1100
Then the binary code is:1100
ID:0-3
Then1100
Add one0
, Changes01100
To start the operation.
0
Exclusive or1
1.
1
Exclusive or1
0.
1
Exclusive or0
1.
0
Exclusive or0
0.
So Gray code is1010
.
Gray code to decimal
Now let's assume that we get a gray code1010
. How to decode to decimal. From the second digit on the left, the decoded value of each digit is the same or different from the decoded value of the first digit on the left, and the decoded value is used as the decoded value (the leftmost digit remains unchanged ). Returns or in sequence until the second bit. The value after conversion (Binary Number) is the value of the binary code after Gray code conversion.
Therefore, the operation result is:
0
And first1
Returns 1
Result1
And second place0
The XOR result is 1.
Result1
And third place1
The XOR result is 0.
Result0
And fourth place0
The XOR result is 0.
So the binary value is:1100
-> Decimal:12
.
Code Implementation
The code implementation is put below, and only the encoding part is put in this code without decoding. If there is a problem with decoding, you can comment to me.
//// Main. cpp // GrayCode_leetcode // Created by Alps on 14/12/7. // Copyright (c) 2014 chen. All rights reserved. // # include
# Include
# Include
# Include
Using namespace std; class Solution {public: vector
GrayCode (int n) {vector
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
Test; Solution sl; test = sl. grayCode (3); vector
: Iterator iter; for (iter = test. begin (); iter! = Test. end (); iter ++) {printf ("% d \ n", * iter);} return 0 ;}