Friday, June 1, 2012

A simple method for Conversion of Gray code to Binary and Binary code to Gray with Example



FIRST convert a Gray-coded number to binary by follow this method :-
The binary number and the Gray-coded number will have the same number of bits

1. The binary MSB (left-hand bit) and Gray code MSB will always be the same

2. To get the binary next-to-MSB (i.e. next digit to the right) add the binary MSB and the gray code next-to-MSB.

3. Record the sum, ignoring any carry.

4. Continue in this manner right through to the end.

example:-
Gray Code           Conversion                       Binary Code
                             Gmsb+1       Bmsb
1111                      1                                             1         (1)
1111                      1     +    1= 10                        10       (2), (3)
1111                      1     +    0=   1                        101     (4)
1111                      1     +    1= 10                        1010
Hence 1111 --> 1010

Lets try one more example:-

Gray Code           Conversion                       Binary Code
                             Gmsb+1       Bmsb
0101                     0                                             0       
0101                     1                                             01     
0101                     0     +    1=   1                         011   
0101                     1     +    1= 10                         0110
*(0 is not a significant digit)
Hence 0101 -->0110
Try same for:
Gray Code                 Binary Code
    0000                          0000
    0001                          0001
    0010                          0011
    0011                          0010
    0100                          0111
    0101                          0110
    0110                          0100
    0111                          0101
    1000                         1111
    1001                         1110
    1010                         1100
    1011                         1101
    1100                         1000
    1101                         1001
    1110                         1011
    1111                         1010
               

Now we will convert a Binary-coded number to Gray by following method :-

The binary number and the Gray-coded number will have the same number of bits.

1. Gray MSB (most significant bit) i.e left most and Binary MSB will be same.

2. To get Gray next to MSB (next digit to the right) subtract next MSB(to right) of Binary from its MSB.
 Bmsb - Bmsb+1=G

3. Ignore negative sign.

4. Continue through this manner right through the end

example:-
Binary Code           Conversion                       Gray Code
                             Bmsb       Bmsb+1
1111                      1                                            1         (1)
1111                      1     -     1=   0                        10       (2), (3)
1111                      1     -     1=   0                        100     (4)
1111                      1     -     1=   0                        1000
Hence 1111 --> 1000

Here we try one more example :- 


Binary Code           Conversion                       Gray Code
                             Bmsb       Bmsb+1
0101                     0                                            0        
0101                     1                                            01       
0101                     1     -     0=   1                        011    
0101                     0     -     1=   1                        0111
Hence 0101 -->0111

Try some more:-
Binary Code               Gray Code
    0000                          0000
    0001                          0001
    0010                          0011
    0011                          0010
    0100                          0110
    0101                          0111
    0110                          0101
    0111                          0100
    1000                         1100
    1001                         1101
    1010                         1111
    1011                         1110
    1100                         1010
    1101                         1011
    1110                         1001
    1111                         1000