Gray Code to Binary Code in C++
A binary number system in which two successive values differ by just one bit is called grey code, sometimes referred to as reflected binary code. It has uses in a number of industries, such as rotary encoders and digital communications. In digital electronics and computer programming, converting Grey code to binary code is a frequent process. Using the Gray-to-Binary conversion process and iterating through the Grey code bits, you may write a C++ program that converts Grey code to binary code. Every Grey code bit is XORed with the subsequent higher-order bit as part of the process. Moving from the most important to the least important bit in order of significance
Gray to Binary Conversion:
- The binary code's Most Significant Bit (MSB) and the grey code's MSB are always the same.
- The binary code bit at that position and the preceding index can be XORed to generate further bits of the output grey code.
EX: Binary: 0011
Gray : 0010
To convert binary 0011 to Gray code:
- The leftmost bit remains the same (0).
- For the second bit, XOR is with the first bit in the binary code (0 XOR 0 = 0).
- For the third bit, XOR is with the second bit in the binary code (0 XOR 1 = 1).
- For the fourth bit, XOR is with the third bit in the binary code (1 XOR 1 = 0).
- So, the Gray code 0010 is indeed correct for the binary code 0011.
Binary: 01001
Gray: 01101
To convert binary 01001 to Gray code:
- The leftmost bit remains the same (0).
- For the second bit, XOR is with the first bit in the binary code (0 XOR 1 = 1).
- For the third bit, XOR is with the second bit in the binary code (1 XOR 0 = 1).
- For the fourth bit, XOR is with the third bit in the binary code (1 XOR 0 = 1).
- For the fifth bit, XOR is with the fourth bit in the binary code (1 XOR 1 = 0).
- So, the Gray code 01101 is indeed correct for the binary code 01001.
Example:
Let's take an example to illustrate how to convert gray code to binary code in C++.
#include <iostream>
#include <bitset>
using namespace std;
// Function to convert Gray code to binary code
int grayToBinary(int gray)
{
int binary = gray;
while(gray >>= 1) {
binary ^= gray;
}
return binary;
}
int main() {
int grayCode1 = 11; // Replace with any Gray code
int grayCode2 = 7; // Replace with another Gray code
int binaryCode1 = grayToBinary(grayCode1);
int binaryCode2 = grayToBinary(grayCode2);
cout << "Gray code 1: " << grayCode1 << " -> Binary code 1: " << bitset<8>(binaryCode1) << endl;
cout << "Gray code 2: " << grayCode2 << " -> Binary code 2: " << bitset<8>(binaryCode2) << endl;
return 0;
}
Output:
Gray code 1: 11 -> Binary code 1: 00001101
Gray code 2: 7 -> Binary code 2: 00000101
Explanation:
- Include Headers: The C++ standard library headers are included at the start of the code. Input and output are handled by iostream, and binary values with leading zeros are shown using bitset.
- Function to Convert Grey to Binary:
- The grayToBinary function is defined to convert Grey code to Binary code.
- The grey code that has to be converted is represented by an integer grey that is entered.
- By using the Grey code, the function initializes an integer binary.
- Next, by XORing binary with the shifted grey value, iteratively performs the Gray-to-Binary conversion using a while loop.
- The resulting binary value is the corresponding binary code, and the loop is repeated until grey reaches zero.
- Principal Purpose:
Two Grey Code values (grayCode1 and grayCode2) are defined in the main function. You can convert any Grey code you want to use in place of these values.
- Conversion and Output:
- The program runs the grayToBinary function for both grayCode1 and grayCode2 to save the binary counterparts in binaryCode1 and binaryCode2.
- Subsequently, the original Grey codes and their equivalent binary codes are displayed using cout.
- The binary form is shown as an 8-bit number with leading zeros when bitset<8> is used. The amount of bits can be changed as needed.
Conclusion:
The C++ code that is provided provides a workable way to translate Grey code to binary code. The function grayToBinary, defined in the code, iteratively converts a Grey code into its corresponding binary representation by using a bitwise XOR operation. Different Grey code values can be entered into the main function, and it will produce the matching binary codes. This code is helpful for a variety of applications where converting grey code is necessary because it is flexible and simple to modify. For a variety of uses in digital electronics, telecommunications, and computer science, among other fields, you can effectively execute Gray-to-Binary conversions by changing the Grey code values in the program.