Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute C Online

#include <stdio.h>

#define BYTE_TO_BINARY_PATTERN(n) \
    (n == 10)?"%010d \r\n": \
    (n == 9)?"%09d \r\n": \
    (n == 8)?"%08d \r\n": \
    (n == 7)?"%07d \r\n": \
    (n == 6)?"%06d \r\n": \
    (n == 5)?"%05d \r\n": \
    (n == 4)?"%04d \r\n": \
    (n == 3)?"%03d \r\n": \
    (n == 2)?"%02d \r\n": \
    (n == 1)?"%01d \r\n":"" 
    
#define BYTE_TO_BINARY(byte, n)  \
    0 + \
    (((byte % 2) & 0x1)    * 1) + \
    (((byte / 2) & 0x1)    * 10) + \
    (((byte / 4) & 0x1)    * 100) + \
    (((byte / 8) & 0x1)    * 1000) + \
    (((byte / 16) & 0x1)   * 10000) + \
    (((byte / 32) & 0x1)   * 100000) + \
    (((byte / 64) & 0x1)   * 1000000) + \
    (((byte / 128) & 0x1)  * 10000000) + \
    (((byte / 256) & 0x1)  * 100000000) + \
    (((byte / 512) & 0x1)  * 1000000000) + \
    (((byte / 1024) & 0x1) * 10000000000)

#define POWER_OF_TWO(x) (1 << (x))
#define NUMBER 10

int N = NUMBER;
int Tab[POWER_OF_TWO(NUMBER)];

void GenerateGreyCode(int N, int* pTab)
{
    int max = 1 << N, i = 0;
    for(i = 0; i < max; ++i)
    {
        pTab[i] = i ^ (i >> 1);
    }
}

int main()
{
    int i = 0;

    GenerateGreyCode(N, Tab);

    for(i = 0; i < POWER_OF_TWO(NUMBER); ++i)
    {
        printf(BYTE_TO_BINARY_PATTERN(N), BYTE_TO_BINARY(Tab[i], N));
    }

    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.