# Construction of Code Converters

In this tutorial, we are going to learn about the Construction of Code Converters: **Designing of Binary to Gray Code converter**, **Designing of Gray to Binary Code converter**.

Submitted by Saurabh Gupta, on January 23, 2020

In this article, we are going to read about **how these code converters are designed and are implemented inside the system?**

## (i) Designing of Binary to Gray Code converter

4-bit binary to gray code converter converts a 4-bit binary number to 4-bit gray code. Binary numbers are provided as input in the form of **B _{3}**

**B**

_{2}**B**

_{1}**B**(MSB is the leftmost and LSB is the rightmost) and we get output in the form of Gray code as

_{0}**G**

_{3}**G**

_{2}**G**

_{1}**G**(MSB is the leftmost and LSB is the rightmost). Thus, the code converter is equivalent to four different logic circuits. One for each of the truth table. The next step is to derive simplified Boolean expressions for each truth table using the K-Map and then we can get the relation between input and output.

_{0}Let's create a conversion table that stores the inputs and their corresponding output for all possible cases of the 4-bit binary number.

Input (Binary Code) | Output (Gray Code) | ||||||
---|---|---|---|---|---|---|---|

B_{3} |
B_{2} |
B_{1} |
B_{0} |
G_{3} |
G_{2} |
G_{1} |
G_{0} |

0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |

0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |

0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |

0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |

0 | 1 | 0 | 1 | 0 | 1 | 1 | 1 |

0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 |

0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 |

1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |

1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 |

1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 |

1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |

1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 |

1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |

1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |

From the truth table, we can observe that,

- The values in
**G**are exactly the same as_{4}**B**, thus we can get_{4}**G**=_{4}**B**._{4} - The values in
**G**is actually the output of XOR operation between the_{3}**B**and_{4}**B**. Thus,_{3}**G**=_{3}**B**⊕_{3}**B**._{3} - The values in
**G**is actually the output of XOR operation between the_{2}**B**and_{3}**B**. Thus,_{2}**G**=_{2}**B**⊕_{2}**B**._{2} - The values in
**G**is actually the output of XOR operation between the_{1}**B**and_{2}**B**. Thus,_{1}**G**=_{1}**B**⊕_{1}**B**._{1}

We can verify our observation by solving and obtaining Boolean expression from the K-Map for each of Gray code bits, we already know by convention **1s** are the minterms for K-Maps, thus K-map can be solved as,

Thus, the logic circuit for 4-bit Binary to Gray code can be drawn as,

### (ii) Designing of Gray to Binary Code converter

Gray code to Binary code converter can be designed in the same procedure as we have followed above. For all the possible cases of input (4-bit Gray code) **G _{3}**

**G**

_{2}**G**

_{1}**G**, we need to obtain simplified expressions for output (4-bit Binary code)

_{0}**B**

_{3}**B**

_{2}**B**

_{1}**B**by solving the K-Map. All possible inputs and outputs can be summarized in a truth table as,

_{0}Gray Code (Input) | Binary Code (Output) | ||||||
---|---|---|---|---|---|---|---|

G_{3} |
G_{2} |
G_{1} |
G_{0} |
B_{3} |
B_{2} |
B_{1} |
B_{0} |

0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |

0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |

0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |

0 | 1 | 0 | 0 | 0 | 1 | 1 | 1 |

0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |

0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 |

0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 |

1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |

1 | 0 | 0 | 1 | 1 | 1 | 1 | 0 |

1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 |

1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 |

1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |

1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |

1 | 1 | 1 | 0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 |

Solving K-Map to get the required Boolean expressions,

Logic Diagram for Gray to Binary converter can be drawn as,

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.