0% found this document useful (0 votes)
22 views

Code Converters - Binary To_from Gray Code - GeeksforGeeks

This document discusses code converters that translate between binary code and Gray code, defining both types of codes and detailing the conversion processes. It includes explanations of the digital circuits and algorithms used for these conversions, along with applications in rotary encoders, control systems, and signal processing. The article concludes with a summary of the key concepts and methods involved in binary to Gray code conversion and vice versa.

Uploaded by

Riyan Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Code Converters - Binary To_from Gray Code - GeeksforGeeks

This document discusses code converters that translate between binary code and Gray code, defining both types of codes and detailing the conversion processes. It includes explanations of the digital circuits and algorithms used for these conversions, along with applications in rotary encoders, control systems, and signal processing. The article concludes with a summary of the key concepts and methods involved in binary to Gray code conversion and vice versa.

Uploaded by

Riyan Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

Aptitude Engineering Mathematics Discrete Mathematics Operating System DBMS Computer Netw

Code Converters – Binary to/from Gray Code


Last Updated : 03 Jul, 2024

In this article, we will go through Code Converters – Binary to/from Gray


Code,we will start our article by defining Code converters, Binary code
and Gray code, and then we will go through the conversion of binary
code to gray code and vice versa. At last, we will conclude our article
with some faqs.

Table of Content
Code Converters
Binary Code
Gray Code
Converting Binary to Gray Code
Converting Gray Code to Binary
Application

What are Code Converters?


Code Converters are the digital circuits or algorithms that are designed
to translate data representation from one format to the other format. In
this article, we will go through binary to gray code converters. The
binary-to-Gray code converter takes binary input and translates it to its
corresponding gray code representation.

For more about conversions please go through Number System and


base conversions.

What is a Binary Code?

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 1/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

The Binary code is the numerical system used in digital electronics. It


only consists of two Symbols which are 0 and 1.In binary code each
digit is represented by power of 2 with the starting bit (right most bit)
as 20 and bit as 21 and so on.The binary code is serves as the basis for
encoding text,number and various other types of data in the digital
devices.

What is a Gray Code?


Gray Code system is a binary number system in which every successive
pair of numbers differs by only one bit. It is used in applications in which
the normal sequence of binary numbers generated by the hardware ,is
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.

Another Name of Gray Code

Unity Hamming Distance Code


Cyclic Code
Reflecting Code

Converting Binary to Gray Code


Let

b0 , b1 , b2 , and b3
​ ​ ​ ​

be the bits representing the binary numbers, where

b0 ​

is the LSB and

b3 ​

is the MSB, and Let

g0 , g1 , g2 , and g3
​ ​ ​ ​

be the bits representing the gray code of the binary numbers, where

g0 ​

is the LSB and


https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 2/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

g3 ​

is the MSB. The truth table for the conversion is

[Tex] \begin{tabular}{||c|c|c|c||c|c|c|c||} \hline \multicolumn{4}{||c||}


{Binary} & \multicolumn{4}{|c||}{Gray Code}\\ \hline b_3 & b_2 & b_1 &
b_0 & g_3 & g_2 & g_1 & g_0 \\ \hline \hline 0 & 0 & 0 & 0 & 0 & 0 & 0 &
0 \\ \hline 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 \\ \hline 0 & 0 & 1 & 0 & 0 & 0 &
1 & 1 \\ \hline 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 \\ \hline \hline 0 & 1 & 0 & 0
& 0 & 1 & 1 & 0 \\ \hline 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1 \\ \hline 0 & 1 & 1
& 0 & 0 & 1 & 0 & 1 \\ \hline 0 & 1 & 1 & 1 & 0 & 1 & 0 & 0 \\ \hline \hline
1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 \\ \hline 1 & 0 & 0 & 1 & 1 & 1 & 0 & 1 \\
\hline 1 & 0 & 1 & 0 & 1 & 1 & 1 & 1 \\ \hline 1 & 0 & 1 & 1 & 1 & 1 & 1 &
0 \\ \hline \hline 1 & 1 & 0 & 0 & 1 & 0 & 1 & 0 \\ \hline 1 & 1 & 0 & 1 & 1
& 0 & 1 & 1 \\ \hline 1 & 1 & 1 & 0 & 1 & 0 & 0 & 1 \\ \hline 1 & 1 & 1 & 1
& 1 & 0 & 0 & 0 \\ \hline \hline \end{tabular} [/Tex]
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

g0 ​

K-map for

g1 ​

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 3/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

K-map for

g2 ​

K-map for

g3 ​

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 4/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

Corresponding Minimized Boolean Expressions and Digital


Circuit for Gray Code Bits

g0 = b0 b′1 + b1 b′0 = b0 ⊕ b1
​ ​ ​ ​ ​ ​ ​

g1 = b2 b′1 + b1 b′2 = b1 ⊕ b2
​ ​ ​ ​ ​ ​ ​

g2 = b2 b′3 + b3 b′2 = b2 ⊕ b3
​ ​ ​ ​ ​ ​ ​

g3 = b3 ​ ​

Generalized Boolean Expression for conversion of Binary to Gray


Code

Boolean expression for conversion of binary to gray code for n-bit :

Gn= Bn

G n-1 = B n XOR B n-1 : :

G 1 = B 2 XOR B 1

Converting Gray Code to Binary


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

b0 , b1 , b2 , and b3
​ ​ ​ ​

be the bits representing the binary numbers, where

b0 ​

is the LSB and

b3 ​

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 5/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

is the MSB, and Let

g0 , g1 , g2 , and g3
​ ​ ​ ​

be the bits representing the gray code of the binary numbers, where

g0 ​

is the LSB and

g3 ​

is the MSB. Truth table

[Tex] \begin{tabular}{||c|c|c|c||c|c|c|c||} \hline \multicolumn{4}{||c||}{Gray


Code} & \multicolumn{4}{|c||}{Binary}\\ \hline g_3 & g_2 & g_1 & g_0 &
b_3 & b_2 & b_1 & b_0\\ \hline \hline 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\hline 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 \\ \hline 0 & 0 & 1 & 0 & 0 & 0 & 1 &
1 \\ \hline 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 \\ \hline \hline 0 & 1 & 0 & 0 & 0
& 1 & 1 & 1 \\ \hline 0 & 1 & 0 & 1 & 0 & 1 & 1 & 0 \\ \hline 0 & 1 & 1 & 0
& 0 & 1 & 0 & 0 \\ \hline 0 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \\ \hline \hline 1 &
0 & 0 & 0 & 1 & 1 & 1 & 1 \\ \hline 1 & 0 & 0 & 1 & 1 & 1 & 1 & 0 \\ \hline
1 & 0 & 1 & 0 & 1 & 1 & 0 & 0 \\ \hline 1 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\
\hline \hline 1 & 1 & 0 & 0 & 1 & 0 & 0 & 0 \\ \hline 1 & 1 & 0 & 1 & 1 & 0
& 0 & 1 \\ \hline 1 & 1 & 1 & 0 & 1 & 0 & 1 & 1 \\ \hline 1 & 1 & 1 & 1 & 1
& 0 & 1 & 0 \\ \hline \hline \end{tabular} [/Tex]
Using K-map to get back the binary bits from the gray code – K-map for

b0 ​

K-map for

b1 ​

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 6/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

K-map for

b2 ​

K-map for

b3 ​

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 7/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

Corresponding Boolean expressions

[Tex] \begin{align*} b_0 &=g_3^\prime g_2^\prime g_1^\prime g_0 +


g_3^\prime g_2^\prime g_1g_0^\prime + g_3^\prime g_2g_1^\prime
g_0^\prime + g_3^\prime g_2g_1g_0 +g_3g_2^\prime g_1^\prime
g_0^\prime + g_3g_2^\prime g_1g_0 \\ &\:\:\:+g_3g_2g_1^\prime g_0 +
g_3g_2g_1g_0^\prime \\ &= g_3^\prime g_2^\prime( g_1^\prime g_0 +
g_1g_0^\prime) + g_3^\prime g_2(g_1^\prime g_0^\prime + g_1g_0)
+g_3g_2^\prime(g_1^\prime g_0^\prime + g_1g_0 )\\ &\:\:\:+g_3g_2
(g_1^\prime g_0 + g_1g_0^\prime) \\ &= g_3^\prime
g_2^\prime(g_0\oplus g_1) + g_3^\prime g_2(g_0\odot
g_1)+g_3g_2^\prime(g_0\odot g_1) + g_3g_2 (g_0\oplus g_1) \\ &=
(g_0\oplus g_1)(g_2\odot g_3) + (g_0\odot g_1)(g_2\oplus g_3)\\ &=
g_3\oplus g_2\oplus g_1\oplus g_0\\ b_1 &= g_3^\prime g_2^\prime
g_1 + g_3^\prime g_2g_1^\prime + g_3g_2g_1 + g_3g_2^\prime
g_1^\prime \\ &= g_3^\prime(g_2^\prime g_1 + g_2g_1^\prime) +
g_3(g_2g_1 + g_2^\prime g_1^\prime) \\ &= g_3^\prime(g_2\oplus g_1)
+ g_3(g_2\odot g_1) \\ &= g_3\oplus g_2\oplus g_1\\ b_2 &= g_3^\prime
g_2 + g_3g_2^\prime\\ &= g_3\oplus g_2\\ b_3 &= g_3 \end{align*}
[/Tex]

Corresponding Digital Circuit

The Circuit can be given as

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 8/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

Generalized Boolean Expression for conversion of Gray to Binary


Code

Boolean expression for conversion of gray to binary code for n-bit :

Bn= Gn

B n-1 = B n XOR G n-1 = G n XOR G n-1 : :

B 1 = B 2 XOR G 1 = G n XOR ………… XOR G 1

Application of Code Converters – Binary to/from Gray


Code
Given Below are the Applications of the Code Converters – Binary
to/from Gray Code

Rotary Encoders:The Rotary encoders are device which is used to


convey rotary motions into digital signals.The code converters use to
convert the gray code output of rotary encodes into binary.
Control Systems:The Gray code converters are used in the Control
system to integrate between system using binary and gray code.
Signal Processing:The Code converters are used in the signal
processing to translate between binary and gray coded signals.
Analog-to-Digital Conversion:The code converters are used in Adc
to interface between gray coded ADC outputs and binary based DSP
systems.

Conclusion
In this Article we have gone through Code Converters – Binary to/from
Gray Code,we have gone through the definition of the Code
converters,Binary code and Gray code,then we have gone through Code
conversion between Binary to Gray code and vice versa.

https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 9/15
21/02/2025, 18:35 Code Converters - Binary to/from Gray Code - GeeksforGeeks

Code Converters – Binary to/from Gray Code – FAQs

How do you minimize the logic gates required for a Binary-to-


Gray Code Converter circuit?

To reduce the logic gates the techniques like Boolean algebra


simplification, Karnaugh maps and logic gate optimization
algorithms can be used.

Can Code Converters introduce latency in digital systems ?

Yes,code converters bring latency due to the processing time


required for the conversion.

Can Code Converters be implemented using reversible logic


gates for applications requiring energy-efficient computing?

Yes the code converters can be designed using reversible logich


which will have the property of information losslessness and less
minimal power.

This article is contributed by

Chirag Manwani

. If you like GeeksforGeeks and would like to contribute, you can also
write an article using

write.geeksforgeeks.org
or mail your article to [email protected]. See your article
appearing on the GeeksforGeeks main page and help other Geeks.

Dreaming of M.Tech in IIT? Get AIR under 100 with our GATE 2026
CSE & DA courses! Get flexible weekday/weekend options, live
https://www.geeksforgeeks.org/code-converters-binary-to-from-gray-code/ 10/15

You might also like