Algorithmic learning-Interpretation of gray Code (grey code) and C + + implementation

Source: Internet
Author: User
Tags pow

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?

    1. First, the decimal number is converted into binary form.
    2. For n-bit binary code words, from right to left, numbered 0 to n-1.
    3. 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:1100Number is0-3
And then in1100Pre-fill One0, it becomes01100To start the operation.

0XOR or1to 1.
1XOR or1to 0.
1XOR or0to 1.
0XOR or0to 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:

0With the first bit1Make an XOR or result of 1
Above results1With the second digit0XOR result is 1
Above results1and third place1XOR result is 0
Above results0With fourth digit0XOR 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.