# Binary to Grey code conversion & Grey to binary

In this article we will cover Grey Code, binary to grey code conversion and Grey code to binary conversion. Its application, advantages and  disadvantages.

Grey Code is named after scientist Frank Grey.

## What is a Grey Code ?

A grey code is a binary number system that is ordered in a way that every increasing value gets differed by only one bit. A Grey code is also known as Reflected Binary Code ( RBC ) or cyclic code.

In other words we can say that grey codes differ by only one bit from their upper or lower number.

Although grey codes are the most popular unit distance codes, still it is not suitable in arithmetic operations. Usually grey code is used in error detection of digital communication and analogue to digital converters. Initially students find grey code difficult to understand but it is really easy.

### Binary to Grey Code Converter

A digital logical circuit that converts BCD to grey code is known as Binary to grey code converter. Binary ( BCD ) is converted to grey code using XOR gate.

Truth table for XOR gate

When inputs are similar, output is zero and when inputs are different output is logic 1.

### Binary to Grey Code Converter Circuit

The table below shows 3 bit BCD numbers and their grey code, side by side.

#### Steps to Convert Binary to Grey Code

Binary to grey code conversion is done by following steps

1. Copy the row B2 in row G2. Row B2 and G2 will be similar.
2. Now, G1 is obtained by B2 ⨁ B1. When B2 and B1 will be same, the output will be zero and B2 and B1 are different the output is 1.
3. G0  is obtained by B1 ⨁ B0. When B1 and B0 are similar, the output will be zero and B1 and B0 are different the output is 1.

Example 1

Suppose we need to convert BCD 1001 to Grey Code.

1. The most significant bit ( MSB ) is 1. So the first digit will be reflected as it is.
2. First and second bit is 1 & 0 then 1 ⨁ 0 will be 1, since it has two different inputs.
3. Secondly, second and third bits are 0 & 0. So 0 ⨁ 0 will be 0 because it has same inputs.
4. At last, third bit and fourth bit are 0 & 1. So 0 ⨁ 1 will give output 1.
5. Finally, The Grey Code will be 1101 for BCD 1001.

Example 2

#### Q. Convert BCD 0110 to Grey Code

1. Again the first digit 0 will be reflected as it is.
2. Now the first and second bit is 0 & 1 so 0 ⨁ 1 is 1. Different inputs give output 1.
3. In the third step, the second and third input bits are 1 & 1 so 1 ⨁ 1 is 0. Similar inputs give output 0.
4. At last, the third and fourth input bits are 1 & 0 so 1 ⨁ 0 will be 1.
5. Finally the Grey code for 0110 is 0101.

Example 3

#### Q. Convert decimal 15 into grey code.

Steps to convert decimal to grey code is –

1. First of all convert decimal to BCD.
2. BCD for decimal 15 is 1111.
3. First digit 1 will be reflected as it is.
4. Now first and second bit is 1 & 1. So 1 ⨁ 1 is 0.
5. Second and third bit is also 1 & 1. So 1 ⨁ 1 is again 0.
6. The third and fourth bits are again 1 & 1. So 1 ⨁ 1 is again 0.
7. Finally, the Grey code for decimal 15 ( 1111 ) is 1000.

## Grey Code to Binary Conversion

We have successfully converted BCD to Grey Code. Now lets convert Grey Code back to BCD.

A digital logical circuit that converts Grey code to Binary is known as Grey code to binary converter. Grey code is also converted to binary using XOR gate.

### Grey Code to Binary Converter Circuit

The table below shows 3 bit grey code and their Binary number, side by side.

Here, D denotes Decimal number. G2, G1 & G0 are Grey codes and B2, B1 & B0 are Binary Coded Decimal.

#### Steps to Convert Grey Code to Binary

Grey code is converted into binary in the same way as binary was converted into grey. Only the last step is different.

1. G2 gets reflected into B2 as it is.
2. B1 is obtained by G2 ⨁ G1.
3. Now, B0 is obtained by G2 ⨁ G1 ⨁ G0. Only this step is different.

Example

#### Q. Convert Grey code ( 111 ) to binary.

1. The first bit 1 will be reflected as it is.
2. B1 will be obtained by XOR of first and second bit ( G2 ⨁ G1 ). So 1 ⨁ 1 = 0 because similar input gives 0 in output.
3. Now, B0 will be obtained by G2 ⨁ G1 ⨁ G0. We know from previous step G2 ⨁ G1 is 0. So now 0 ⨁ G0 = 0 ⨁ 1 = 1.
4. Finally, binary for Grey code ( 111 ) is 101.

### Application of Grey Code

Some applications of Grey Code are –

1. Error detection / correction in digital communication.
2. Analog to Digital converters.
3. Minimize error during analog to digital conversion.
4. Puzzles of mathematics.
5. Position encoders.
6. Circuit minimization of Boolean algebra.

### Advantages of Grey Code

The advantages are –

1. Best for minimizing errors during analogue to digital conversion.
2. Used in clock domain crossing.
3. Minimizing a logic circuit.

### Disadvantages of Grey Code

The disadvantages are –

1. Less practical use other than few common applications.
2. Not applicable in arithmetic operations.

#### Frequently Asked Questions

1.   Why is Grey code known as reflected code ?
• The reason why Grey Code is also called reflected code is shown in table below. Similar colours represent reflection.
 Grey code G2 G1 G0 X 0 0 X 0 1 X 1 1 X 1 0 Mirror X 1 0 X 1 1 X 0 1 X 0 0

The table shows Grey code for 3 bit BCD ( that was explained above in the article ).

Author

Akash Sharma

BCD to Excess 3 code

In this article we are going to discuss about BCD to access 3 code conversion. Before we start, make sure you have a clear concept of number system and decimal to binary conversion.

NAND NAND Realization

NAND NAND realization is a concept under which we draw a logic circuit by using only NAND gates.