Let's Code Gray Code !!

We All know that physical quantities have continuous values, and in order to convert physical quantities to digital systems we need to perform analog-to-digital conversion. However sometimes fast transition from one analog value to another may cause huge error if we use binary system.

For example, take the decimal value 3  which is equivalent to 0011 in binary now let's say that a noise cause the third bit to flipped to 1, now we have 0111 in binary which is equivalent to 7 in decimal , which means that we just jump from 3 to 7 by flipping one bit !!.
In Gray code (named after Frank Gray) only one bit change at time, which is reduces the error in case if one bit flipped in transition from one decimal value to another. The following table shows the conversion from decimal to binary and Gray code

                                            Decimal         Binary     Gray Code
                                              0         0000     0000
                                              1         0001     0001
                                              2         0010     0011
                                              3             0011     0010
                                              4         0100     0110
                                              5         0101     0111
                                              6         0110     0101
                                              7         0111     0100
                                              8         1000     1100
                                              9         1001     1101
                                              10         1010     1111
                                              11         1011     1110
                                              12         1100     1010
                                              13         1101     1011
                                              14         1110     1001
                                              15         1111     1000


From the table we can notice that only one bit get flipped at a time, take for example the transition from 5 to 6 in binary we need to flip two bits while in Gray code only the
second bit get flipped. Now we want to know how we can decide to flip a bit or not in Gray code. The conversion flows the following formula :

equation
Conversion from decimal to Gray code










Let's take the decimal value 2 for example which is 0010 in binary, so the first step we need to shift the value to the right by 1, so we will have 0001, last we need to perform XOR operation (0010) XOR (0001) which is equals to (0011) in Gray code. Using this formula  can convert any unsiged int number to Gray code equivalent value. Now let's write a c code that can convert decimal values to Gray code the values will be return in decimal representation so use can use the given table to check the values


#include <stdio.h>
#include <stdint.h>
unsigned int DecimalToGray(unsigned int num) 
{
    return (num ^ (num >> 1));
}


int main(int argc, char **argv) 
{

    for (unsigned int i=0;i<16; i++)
    {
        printf("Decimal value: %u, Gray code equivalent: %u\n",i,DecimalToGray(i));
    }
    
    
    
    return 0;
}