What is Code Converter : Binary to Gray Code & Gray Code to Binary Conversion
In computers, we need to convert binary to gray and gray to binary. The conversion of this can be done by using two rules namely binary to gray conversion and gray to binary conversion. In the first conversion, the MSB of the gray code is constantly equivalent to the MSB of the binary code. Additional bits of the gray code’s output can get using the EX-OR logic gate concept to the binary codes at that present index as well as the earlier index. Here MSB is nothing but the most significant bit. In the first conversion, the MSB of the binary code is constantly equivalent to the MSB of the particular binary code. Additional bits of the binary code’s output can get using the EX-OR logic gate concept by verifying gray codes at that present index. If the present gray code bit is zero then after that copy earlier binary code, as well copy reverse of earlier binary code bit. This article discusses an overview of code converters which includes binary to gray code converter as well as gray to binary code converter.
What is a Binary Code?
In digital computers, the code which is used based on a binary number system is known as binary code. There are two possible states like ON & OFF that are represented through 0 & 1. The digital system uses 10 digits where every position of digit signifies the power of 10. In a binary system, every position of a digit represents a power of 2.
A binary code signal includes a sequence of electrical pulses that signify characters, numbers & operations to be executed. A clock device is used to transmit normal pulses, as well as components like transistors, turn ON/Turn OFF to flow otherwise blocks the signals. In binary code, every decimal number ranges from 0 to 9 can be signified through a set of 4-binary bits/ digits. The basic 4 arithmetic operations like addition, subtraction, multiplication &division can all be decreased to combinations of basic Boolean algebraic functions on binary numbers.
What is the Gray Code?
The Gray Code or RBC (reflected binary code), or cyclic code is a series of binary number systems. The main reason to call this reflected binary code is the initial N/2 values are in reverse order as compare with the last N/2 values. In this kind of code, the two successive values are changed through a single bit of binary digits. These codes are mainly used in the common series of binary numbers generated by hardware.
The binary numbers can cause errors once the transition is done from a single number to consecutive. This type of code basically solves this trouble by altering simply one bit once the change between the numbers is done.
This kind of code is extremely light weighted & it doesn’t depend on the digit value that is stated throughout the position. This kind of code is also named a cyclic variable code because the change of a single value to its consecutive value holds a change of a single bit only.
This is the most popular for the unit distance codes; however, it is not appropriate for arithmetic functions. The applications of gray code include analog to digital converters and digital communication for error correction. Firstly, gray code is not easy to understand, however, turns into very easier to recognize.
Binary to Gray Code Converter
Binary code is a very simple representation of data using two values such as 0’s and 1’s, and it is mainly used in the world of the computer. The binary code could be a high (1) or low (0) value or else even a modify in value. Gray code or reflected binary code estimates the binary code nature that is arranged with on & off indicators, usually denoted with ones & zeros. These codes are used to look at clarity as well as error modification in binary communications.
The conversion of binary to gray code can be done by using a logic circuit. The gray code is a non-weighted code because there is no particular weight is assigned for the position of the bit. An n-bit code can be attained by reproducing an n-1 bit code on an axis subsequent to the rows of 2^{n-1}, as well as placing the most significant bit of 0 over the axis with the most significant bit of 1 beneath the axis. The step by step gray code generation is shown below.
This method uses an Ex-OR gate to perform among the binary bits. The following best example will be very useful for knowing the conversion of binary to gray. In this conversion method, take down the MSB bit of the present binary number, as the primary bit or MSB bit of the gray code number is similar to the binary number.
To get the straight gray coded bits for generating the corresponding gray coded digit for the given binary digits, add the primary digit or the MSB digit of binary number toward the second digit & note down the product next to the primary bit of gray code, and add the next binary bit to third bit then note down the product next to the 2^{nd} bit of gray code. Similarly, follow this procedure until the final binary bit as well as note down the outcomes depending on EX-OR logic operation to generate the corresponding gray coded binary digit.
Example of Binary to Gray Code Converter
Let assume the Binary code digits be bo, b1, b2, b3 whereas the particular Gray Code can be attained based on the following concept.
From the above operation, finally we can get the gray values like g3 = b3, g2 = b3 XOR b2, g1= b2 XOR b1, g0 = b1 XOR b0.
For example take the binary value b3, b2, b1, b0 = 1101 and find the gray code g3, g2, g1, g0 based on the above concept
g3=b3=1
g2 = b3 XOR b2 = 1 XOR 1 =0
g1= b2 XOR b1= 1 XOR 0 = 1
g0= b1 XOR b0= 0 XOR 1 = 1
The final gray code for the value of binary 1101 is 1011
Binary to Gray Code Converter Table
Decimal Number | Binary Code | Gray Code |
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 |
VHDL Code for Binary to Gray Code Conversion is given below.
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
entity bin2gray is
port( bin : in std_logic_vector(3 downto 0); –binary input
G : out std_logic_vector(3 downto 0) –gray code output
);
end bin2gray;
architecture gate_level of bin2gray is
begin
–xor gates.
G(3) <= bin(3);
G(2) <= bin(3) xor bin(2);
G(1) <= bin(2) xor bin(1);
G(0) <= bin(1) xor bin(0);
end;
Advantages
The advantages of binary code include the following.
- The main benefit of using binary code is that it is simply signified through electronic devices
- Binary data is also very simple to store.
- Very easy to signify & control electronically & mechanically.
- The disparity among representations of symbols can be increased so error possibility can be reduced.
The disadvantages of binary code include the following.
- The required number of symbols can be increased to signify a given number of overall position value systems.
- Humans cannot read them extremely effectively because of their length & and using base-ten numbers through default
- It uses many digits to signify any logical number
Applications
The applications of binary code include the following.
- Binary codes are used in telecommunications as well as computing for different techniques of data encoding like character strings to bit strings. The width used by these methods is fixed otherwise variable-width strings.
- This is used in computer languages as well as programming because computer languages mainly depend on 2-digit number systems.
Gray to Binary Code Converter
This gray to binary conversion method also uses the working concept of the EX-OR logic gate among the bits of gray as well as binary bits. The following example with step by step procedure may help to know the conversion concept of gray code to binary code.
To change gray to binary code, take down the MSB digit of the gray code number, as the primary digit or the MSB of the gray code is similar to the binary digit.
To get the next straight binary bit, it uses the XOR operation among the primary bit or MSB bit of binary to the next bit of the gray code.
Similarly, to get the third straight binary bit, it uses the XOR operation among the second bit or MSB bit of binary to the third MSD bit of the gray code and so on.
Example of Gray to Binary Code Converter
Let assume the Gray Code digits g3, g2, g1, g0 whereas the particular Binary code digits are bo, b1, b2, b3 can be attained based on the following concept.
From the above operation, finally we can get the binary values like b3 = g3, b2 = b3 XOR g2, b1= b2 XOR g1, b0 = b1 XOR g0.
For example take the gray value g3, g2, g1, g0 = 0011 and find the binary code b3, b2, b1, b0 based on the above concept
b3=g3=0
b2 = b3 XOR g2 = 0 XOR 0 =0
b1= b2 XOR g1= 0 XOR 1 = 1
b0= b1 XOR g0= 1 XOR 1 = 0
The final binary code for the value of gray 0011 is 0010
Gray to Binary Code Converter Table
Decimal Number | Gray Code | Binary Code |
0 | 0000 | 0000 |
1 | 0001 | 0001 |
2 | 0010 | 0010 |
3 | 0011 | 0011 |
4 | 0110 | 0100 |
5 | 0111 | 0101 |
6 | 0101 | 0110 |
7 | 0100 | 0111 |
8 | 1100 | 1000 |
9 | 1101 | 1001 |
10 | 1111 | 1010 |
11 | 1110 | 1011 |
12 | 1010 | 1100 |
13 | 1011 | 1101 |
14 | 1001 | 1110 |
15 | 1000 | 1111 |
Advantages
The advantages of gray code include the following.
- The logic circuit can be reduced
- Used in crossing the clock domain
- Used for minimizing the error while changing the signals from analog to digital
- Once it is used within genetic algorithms, hamming wall occurrence can be reduced.
Disadvantages
The disadvantages of gray code include the following.
- Not appropriate for arithmetic functions
- Applicable for few precise applications
Applications
The applications of gray code include the following.
- It is used in analog to digital converters
- In digital communication for correction of an error
- It reduces errors while changing the signals from analog to digital.
- Mathematical puzzles
- Minimization of a Boolean circuit
- It is used for communication between two clock domains
- Genetic algorithms
- Position encoders
VHDL Code for Gray code to Binary Conversion is given below.
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
entity gray2bin is
port( G : in std_logic_vector(3 downto 0); –gray code input
bin : out std_logic_vector(3 downto 0) –binary output
);
end gray2bin;
architecture gate_level of gray2bin is
begin
–xor gates.
bin(3) <= G(3);
bin(2) <= G(3) xor G(2);
bin(1) <= G(3) xor G(2) xor G(1);
bin(0) <= G(3) xor G(2) xor G(1) xor G(0);
end;
3 Bit Binary to Gray Code Converter
Assume the binary digits in 3-bit binary number like b0 , b1 , b2, wherever the ‘b2’ bit is the MSB (most significant bit) & the ‘b0’ bit is the LSB (least significant bit) of Binary. The digits of Gray code are g0, g1, g2 , wherever the ‘g2’ digit is the MSB (most significant bit) whereas the digit ‘g0’ is the LSB (least significant bit) of Gray code.
Binary Code – b2,b1,b0 | Gray Code – g2,g1,g0 |
000 | 000 |
001 | 001 |
010 | 011 |
011 | 010 |
100 | 110 |
101 | 111 |
110 | 101 |
111 | 100 |
Thus, the boolean expression can be solved for binary to gray code converter using k-map, we can get g2 = b2, g1 = b1⊕ b2 & g0 = b0 ⊕ b1. Likewise, we can change n-bit binary number (bnb (n-1) …b2 b1 b0) into Gray code ( gng (n-1) …g2 g1 g0).
For LSB (least significant bit)
g0 = b0⊕b1
g1= b1⊕b2
g2 = b1⊕b2
g(n-1) = b (n-1) ⊕ bn, gn = bn.
For instance, convert 111010 binary numbers into Gray code.
So based on the above algorithm,
g0 = b0 ⊕ b1 => 0 ⊕ 1 = 1
g1=b1 ⊕ b2 = 1 ⊕ 0 = 1
g2 = b2 ⊕ b3 = 0 ⊕1 = 1
g3 = b3 ⊕ b4 = 1⊕1 = 0
g4 = b4 ⊕ b5 = 1 ⊕ 1 = 0
g5 = b5 = 1 = 1
So, the conversion of binary to gray code will be – 100111.
Binary to Gray Code Converter using IC 7486
The conversion of binary to gray and gray to binary can be done using IC7486. The required components for making this is a breadboard, connecting wires, LEDs, resistors, XOR (IC7486), push-button switches & a battery for the power supply.
The package of IC7486 mainly includes four XOR logic gates, where pins 7 and 14 will provide supply for all the logic gates. The o/ps of a single XOR gate is connected to the other logic gate’s input within the same or other chip until they share a similar ground terminal.
Thus, this is all about binary to gray code converter and gray to binary code converter. From the above information finally, we can conclude that these converters play an essential role in performing different operations of digital electronics as well as communications among various number systems. The code converter examples which we have discussed above may helpful for understanding the concept of how to do these calculations. Here is a question for you, what are the applications of gray codes?