For Free Articles, Doubts,Any Queries..etc
Mail Us : electricaledition@gmail.com

Conversion from binary code to gray code and gray code to binary code

Conversion from binary code to gray code and gray code to binary code


 In this post let us see conversion from binary code to gray code and gray code to binary code.


Purpose for converting binary code to gray code?

Gray code has occupied a prominent role now-a-days because of its special characteristics. It is, there is change in only one bit for two successive values. This gray code is widely used for error correction in digital communications.

Steps to convert from binary code to gray code:

Let us consider an example to clearly understand the conversion from binary code to gray code

Consider binary code = 1110

Now represent this binary code as b3 b2 b1 b0 = 1 1 1 0

Let the representation of this binary code in gray code be g3 g2 g1 g0

Here b3, g3 are called most significant bits(MSB) and b0,g0 are called least significant bits(LSB).

Step 1: The most significant bit(MSB) of gray code is equal to most significant(MSB) bit of binary code.

In this example g3 = b3 = 1.

Step 2: Now the second most significant bit i.e,which is adjacent to most significant bit(MSB) in the gray code is equal to the sum of most significant bit(MSB) and second most significant bit of binary code. If addition produces any carry ignore the carry.

Note:

Carry is produced when we add two 1's i.e, 1 + 1 = 0, 1 is carry( from Boolean algebra) 

In the following example

g2 = b3 + b2

g2 = 1 + 1 = 0, carry 1 is neglected.

Step 3: The third most significant bit of gray code i.e, which is adjacent to the second most significant bit in the gray code is equal to the sum of second most significant bit and third most significant bit of binary code. If any carry is generated ignore it.

In the considered example:

g1 = b2 +b1

g1 = 1 + 1 = 0, carry 1 is neglected.

Step 4: The above process is continued until the least significant bit(LSB) of gray code is obtained. This least significant bit (LSB) of gray code is obtained by adding last most significant bit and the least significant bit of binary code.If any carry is produced that has to be neglected.

In the following example we have,

g0 = b1 + b0.

g0 = 1 + 0 = 1.

So finally we get the gray code as g3 g2 g1 g0 = 1 0 0 1

Diagrammatically conversion from binary code to gray code  can be represented as follows,

binary code to gray code
Example can be represented diagrammatically as follows,

Conversion from Binary code to Gray Code

 Hence by following above steps conversion from binary code to gray code is done.

Steps to convert from binary code to gray code:


Let us consider an example to clearly understand the conversion from gray code to binary code. 



Consider gray code = 1001.



Now represent this gray code as g3 g2 g1 g0 = 1 0 0 1.

Let the representation of this gray code in binary code be b3 b2 b1 b0.

Here b3, g3 are called most significant bits(MSB) and b0,g0 are called least significant bits(LSB).

Step 1: The most significant bit(MSB) of binary code is equal to most significant(MSB) bit of gray code.

In the example,

b3 = g3 = 1.

Step 2: Now the second most significant bit i.e,which is adjacent to most significant bit(MSB) in the binary code is equal to the sum of most significant bit(MSB) of binary code( It is obtained from step 1) and second most significant bit of gray code. If addition produces any carry ignore the carry.

In this example,

b2 = b3 + g2.

b2 = 1 + 0 = 1.

Step 3: The third most significant bit of binary code i.e, which is adjacent to the second most significant bit in the binary code is equal to the sum of second most significant bit of binary code(It is obtained from step 2)and third most significant bit of gray code. If any carry is generated ignore it.

In the considered example,

b1 = b2 + g1.

b1 = 1 + 0 = 1.

Step 4: The above process is continued until the least significant bit(LSB) of binary code is obtained. This least significant bit (LSB) of binary code is obtained by adding last most significant bit of binary code and the least significant bit of gray code. If any carry is produced that has to be neglected.

In this example,

b0 = b1 + g0.

b0 = 1 + 1 = 0, 1 is carry it is neglected.

So finally we get the binary code as b3 b2 b1 b0 = 1 1 1 0.

Diagrammatically conversion from gray code to binary code can be represented as follows,

Conversion from Gray Code to Binary Code


Example can be represented diagrammatically as follows,

Conversion from Gray Code to Binary Code Example

Hence by following above steps conversion from  gray code to binary code is done.

Today in this post we have learnt conversion from binary code to gray code and gray code to binary code.

This post on conversion from binary code to gray code and gray code to binary code can be downloaded as PDF here.