Binary to Gray Code Converter

This post is to teach you how to convert binary number to corresponding Gray code. The conversion is so simple. You can see it..

If an n bit binary number is represented by Bn Bn-1 ...B1 and its Gray code equivalent by Gn Gn-1...G1 where Bn and Gn are the most significant bits (MSBs), then the Gray code bits are obtained from the binary code as follows. The symbol ⊕ stands for the Exclusive OR (XOR) operation explained below.

The conversion procedure is as follows:




  1. Record the MSB of the binary as the MSB of the Gray code.
  2. Add the MSB of the binary to the next bit in binary, recording the sum and ignoring the carry, if any, i.e, XOR the bits. This sum is the next bit of the Gray code.
  3. Add the 2nd bit of the binary to the 3rd bit of the binary, the 3rd bit to the 4th bit and so on.
  4. Record the successive sums as the successive bits of the Gray code until all the bits of the binary number are exhausted.
Another way to convert a binary number to its Gray code is to Exclusive OR (i.e. to take the modulo sum of) the bits of the binary number with those of the binary number shifted one position to the right. The LSB of the shifted number is discarded and the MSB of the Gray code number is the same as the MSB of the original binary number.

EXAMPLE:
Convert the binary 1001 to Gray code.

Solution:

Binary to Gray code to binary converter conversion how to convert methods shiftiing number encoding representation decode
Two methods to convert binary to Gray code



Method I:
As shown in (a) in image above, record the 8’s bit '1' (MSB) of the binary as the 8’s bit of the Gray code. Then add the 8’s bit of the binary to the 4's bit of the binary (1 + 0-1). Record the sum as the 4's bit of the Gray code. Add the 4's bit of the binary to the 2’s bit of the binary (0 + 0 = 0). Record the sum as the 2’s bit of the Gray code. Add the 2’s bit of the binary to the 1's bit of the binary (0 + 1 = 1). Record the sum as the 1's bit of the Gray code. The resultant Gray code is 1101.


Method II:
As shown in (b) in image above, write the given binary number and add it to the same number shifted one place to the right. Record the MSB of the binary, i.e. 1 as the MSB of the Gray code. Add the subsequent columns (0+1=1 ; 0+0=0 ; 1+0=1) and record the corresponding sum as the subsequent significant bits of the Gray code. Ignore the bit shifted out. The resultant Gray code is 1101.

No comments :

Post a Comment