C Language Gray code related __c language

Source: Internet
Author: User
Tags bitwise pow

Several days to do a number of companies to simulate online evaluation questions, have about gray code related knowledge, a good part to apply to the binary and bitwise operation, these I am not too familiar, today, while Gray code this knowledge are recorded. generation of gray code

There is only one difference between adjacent gray codes, which is to prevent the delay caused by multiple-bit simultaneous changes in the conversion. Specific definitions are as follows.

In the encoding of a group of numbers, if any two adjacent code has only one binary number, this code is called the Gray Code, in addition, because the maximum number and the minimum number of only one digit is different, that is, "end-to-end", so called cyclic code or reflection code. In a digital system, code is often required to change in a certain order. For example, in order to increase the number of natural numbers, if the use of 8421 yards, the number 0111 to 1000 when four-bit will change, and in the actual circuit, 4-bit changes can not be absolutely simultaneous, then the count may appear a short time other code (1100, 1111, etc.). May cause a circuit state error or input error under certain circumstances. You can avoid this error by using gray code. The gray code has a number of coded forms.

If you want to generate an n-bit gray code, then the number of gray code is 2^n. It was wrong to think it was 2n, and later redesigned a function to calculate 2^n, to solve the problem.

Design method:
The first step: Generate 0, 12 strings.
Step two: On the basis of the first step, each string is added 0 and 1, but only one can be added at a time, so it must be done two times. This becomes the 00,01,11,10 (pay attention to symmetry).
The third step: on the basis of the second step, add 0 and 1 to each string, again, add one at a time, and then it becomes 000,001,011,010,110,111,101,100.
All right, so we're going to generate a 3-bit gray code.
If you want to generate 4-bit code, we just need to add another layer of 0,1 to the 3-bit code: 0000,0001,0011,0010,0110,0111,0101,0100,1100,1101,1110,1010,0111,1001,1000.
In other words,n-bit gray code is based on the n-1 bit of gray code generated.
Here is the code, using recursion to generate N-bit gray code.

#include <stdio.h> #include <string.h> #define Max_len 256 char Graycode[max_len][max_len];
    2 ^ n int pow (int m) {int I, sum = 2;
    if (1 = = m) {return 2;
    for (i = 1; i < m i++) {sum *= 2;
return sum;

    } char** graycodecreate (int n) {int i, J;
        if (1 > N) {printf ("wrong!\n");
    return **graycode;
        else if (1 = n) {//printf ("%d", strlen (graycode[0));
        Graycode[0][0] = ' 0 ';
        Graycode[1][0] = ' 1 ';
        printf ("%d", strlen (graycode[0));
    return **graycode;

    } graycodecreate (n-1); for (i = POW (n)/2-1, j = POW (n)/2; I >= 0, J < Pow (n); I--, J + +) {strcpy (graycode[j], graycode[i])
    ; for (i = 0, j = POW (n)/2; i < POW (n)/2, J < POW (n); i++, J + +) {Graycode[i][strlen (Graycode[i])
        ] = ' 0 ';
    Graycode[j][strlen (Graycode[j])] = ' 1 ';
return **graycode;
int main (){int I, j, N;
    scanf ("%d", &n);
    printf ("%d", pow (n));
    Graycodecreate (n); for (i = 0; i < POW (n); i++) {for (j = strlen (Graycode[i))-1; J >= 0; j--) {Putcha
        R (Graycode[i][j]);
    printf ("\ n");
return 0; }
to determine if it can be arranged in a gray-code way.

Give two 8-bit binary code to determine whether the two codes can be arranged sequentially in the form of a gray code. Another way to ask is whether the two binary codes are only one bit different.
By the way, I learned how to output the binary in C language, and also reviewed the relevant usage of bitwise operation. First, two strings of binary code with a different or operation ^, get a new binary code, and then to the new code (1 << i) in the way of bitwise with the Operation &.
Here's a little place to pay attention to, and after the results of the operation can not be directly output, it should be judged whether the result is 0, not 0 output 1, otherwise output 0.
The specific code is as follows:

 #include <stdio.h> #include <stdlib.h> int showinbinary (char term) {int i;
    Int J;
        for (i = 7; I >= 0; i--) {j = (term & (1 << i));
        if (j) {printf ("1");
        else {printf ("0");
    printf ("\ n");
return 0;
    The int isgrayornot (char Term1, char term2) {char ch = term1 ^ Term2;

    int I, j, count = 0;
        for (i = 7; I >= 0; i--) {j = ch & (1 << i);
        if (1 = = j) {count++;
    } if (count) return 1;

    Showinbinary (CH);
return 0;
    int main () {char term1 = 0x9d;

    char term2 = 0x9e;
    printf ("%d", Isgrayornot (Term1, Term2));
    Showinbinary (TERM1);
    Showinbinary (TERM2);
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.