Algorithm learning-Gray Code (Gray Code) Interpretation and c ++ implementation

Source: Internet
Author: User

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:1100ID:0-3
      Then1100Add one0, Changes01100To start the operation.

      0Exclusive or11.
      1Exclusive or10.
      1Exclusive or01.
      0Exclusive or00.
      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:

      0And first1Returns 1
      Result1And second place0The XOR result is 1.
      Result1And third place1The XOR result is 0.
      Result0And fourth place0The 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 ;}
                
               
              
             
            
           
          
         


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.