This C++ program which converts given Gray Code to Binary Code. Both the codes can be stored in a character string. The Binary Code is formed as follows: The most significant bit is same for both, the bit value of next position of Gray Code and previous Binary Code are XORed and stored in next position of Binary Code until we reach the end of Gray Code string.
Here is the source code of the C++ program which converts the given Gray Code to Binary Code. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
* C++ Program to Convert Gray Code to Binary Code
int main(int argc, char* argv)
std::string gray, binary;
std::cout << "Enter the gray code ";
std::cin >> gray;
binary = gray;
for (int i = 0; i < gray.length() - 1; i++)
/* XOR operation */
if (binary[i] == gray[i+1])
binary = binary + "0";
binary = binary + "1";
std::cout << "Gray Code : " << gray << std::endl
<< "Binary Code : " << binary << std::endl;
$ a.out Enter the gray code 1000 Gray Code : 1000 Binary Code : 1111 $ a.out Enter the gray code 0101 Gray Code : 0101 Binary Code : 0110
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Subscribe Now: C++ Programs Newsletter | Important Subjects Newsletters
If you wish to look at all C++ Programming examples, go to C++ Programs.
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for C++ Internship
- Buy Programming Books
- Buy C++ Books
- Buy Computer Science Books
- Practice Programming MCQs