# Binary to Gray code conversion

The Binary to Gray code converter is a logical circuit that is used to convert the binary code into its equivalent Gray code. By putting the MSB of 1 below the axis and the MSB of 1 above the axis and reflecting the (n-1) bit code about an axis after 2^{n-1 }rows, we can obtain the n-bit gray code.

The 4-bit binary to gray code conversion table is as follows:

Decimal Number | 4-bit Binary Code | 4-bit Gray Code |
---|---|---|

ABCD | G_{1}G_{2}G_{3}G_{4} | |

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 |

In 4-bit gray code, the 3-bit code is reflected against the axis drawn after the 2^{4-1}-1^{th} =8^{th} row.

### How to Convert Binary to Gray Code

- In the Gray code, the MSB will always be the same as the 1â€™st bit of the given binary number.
- In order to perform the 2
^{nd}bit of the gray code, we perform the exclusive-or (XOR) of the 1â€™st and 2^{nd}bit of the binary number. It means that if both the bits are different, the result will be one else the result will be 0. - In order to get the 3
^{rd}bit of the gray code, we need to perform the exclusive-or (XOR) of the 2^{nd}and 3^{rd}bit of the binary number. The process remains the same for the 4^{th}bit of the Gray code. Letâ€™s take an example to understand these steps.

### Example

Suppose we have a binary number 01101, which we want to convert into Gray code. There are the following steps which need to perform this conversion:

- As we know that the 1
^{st}bit of the Gray code is the same as the MSB of the binary number. In our example, the MSB is 0, so the MSB or 1^{st}bit of the gray code is 0. - Next, we perform the XOR operation of the 1â€™st and the second binary number. The 1
^{st}bit is 0, and the 2^{nd}bit is 1. Both the bits are different, so the 2^{nd}bit of the Gray code is 1. - Now, we perform the XOR of the 2
^{nd}bit and 3^{rd}bit of the binary number. The 2^{nd}bit is 1, and the 3^{rd}bit is also 1. These bits are the same, so the 3^{rd}bit of the Gray code is 0. - Again perform the XOR operation of the 3
^{rd}and 4^{th }bit of binary number. The 3^{rd}bit is 1, and the 4^{th}bit is 0. As these are different, the 4^{th}bit of the Gray code is 1. - Lastly, perform the XOR of the 4
^{th}bit and 5^{th}bit of the binary number. The 4^{th}bit is 0, and the 5^{th}bit is 1. Both the bits are different, so that the 5^{th}bit of the Gray code is 1. - The gray code of the binary number 01101 is 01011.

## Gray to Binary Code Conversion

The Gray to Binary code converter is a logical circuit that is used to convert the gray code into its equivalent binary code. There is the following circuit used to convert the Gray code to binary number.

Just like Binary to Gray code conversion; it is also a very simple process. There are the following steps used to convert the Gray code into binary.

- Just like binary to gray, in gray to binary, the 1
^{st}bit of the binary number is similar to the MSB of the Gray code. - The 2
^{nd}bit of the binary number is the same as the 1^{st}bit of the binary number when the 2^{nd}bit of the Gray code is 0; otherwise, the 2^{nd}bit is altered bit of the 1^{st}bit of binary number. It means if the 1^{st}bit of the binary is 1, then the 2^{nd}bit is 0, and if it is 0, then the 2^{nd}bit be 1. - The 2
^{nd}step continues for all the bits of the binary number.

### Gray Code to Binary Conversion Example

Suppose we have the Gray code 01011, which we want to convert into a binary number. There are the following steps which we need to perform for the conversion:

- The 1â€™st bit of the binary number is the same as the MSB of the Gray code. The MSB of the Gray code is 0, so the MSB of the binary number is 0.
- Now, for the 2
^{nd}bit, we check the 2^{nd}bit of the Gray code. The 2^{nd}bit of the Gray code is 1, so the 2^{nd}bit of the binary number is one that is altered number of 1^{st} - The next bit of the Gray code is 0; the 3
^{rd}bit is the same as the 2^{nd}bit of the Gray code, i.e., 1. - The 4
^{th}bit of the Gray code is 1; the 4^{th}bit of the binary number is 0 that is the altered number of the 3^{rd} - The 5
^{th}bit of the Gray code is 1; the 5^{th}bit of the binary number is 1; that is the altered number of the 4^{th}bit of the binary number. - So, the binary number of the Gray code 01011 is 01101.

The bits of 4-bit Gray code are considered as G_{4}G_{3}G_{2}G_{1}. Now from the conversion table,

The Karnaugh maps (K-maps) for G_{4}, G_{3}, G_{2, }and G_{1} are as follows: