**Prerequisite –** Number System and base conversions

Gray Code system is a binary number system in which every successive pair of numbers differs in only one bit. It is used in applications in which the normal sequence of binary numbers generated by the hardware may produce an error or ambiguity during the transition from one number to the next.

For example, the states of a system may change from 3(011) to 4(100) as- 011 — 001 — 101 — 100. Therefore there is a high chance of a wrong state being read while the system changes from the initial state to the final state.

This could have serious consequences for the machine using the information. The Gray code eliminates this problem since only one bit changes its value during any transition between two numbers.

### Converting Binary to Gray Code –

Let be the bits representing the binary numbers, where is the LSB and is the MSB, and

Let be the bits representing the gray code of the binary numbers, where is the LSB and is the MSB.

The truth table for the conversion is-

To find the corresponding digital circuit, we will use the K-Map technique for each of the gray code bits as output with all of the binary bits as input.

K-map for –

K-map for –

K-map for –

K-map for –

Corresponding minimized boolean expressions for gray code bits –

The corresponding digital circuit –

### Converting Gray Code to Binary –

Converting gray code back to binary can be done in a similar manner.

Let be the bits representing the binary numbers, where is the LSB and is the MSB, and

Let be the bits representing the gray code of the binary numbers, where is the LSB and is the MSB.

Truth table-

Using K-map to get back the binary bits from the gray code –

K-map for –

K-map for –

K-map for –

K-map for –

Corresponding Boolean expressions –

Corresponding digital circuit –

**References –**

Digital Design, 5th edition by Morris Mano and Michael Ciletti

This article is contributed by **Chirag Manwani**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Binary to Gray code using recursion
- Code Converters - BCD(8421) to/from Excess-3
- Decimal Equivalent of Gray Code and its Inverse
- Need for Intermediate Code and Code Optimization
- Gray to Binary and Binary to Gray conversion
- Three address code in Compiler
- Code Optimization in Compiler Design
- VHDL code for AND and OR Logic Gates
- Register Allocations in Code Generation
- Hamming Code in Computer Network
- Intermediate Code Generation in Compiler Design
- Introduction of Object Code in Compiler Design
- Compiler Design | Detection of a Loop in Three Address Code
- Computer Organization | Locality and Cache friendly code
- Generate n-bit Gray Codes
- Generate n-bit Gray Codes | Set 2
- A backtracking approach to generate n bit Gray Codes
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- 4-bit binary Adder-Subtractor
- Binary Representations in Digital Logic