Binary Numbers is default way to store numbers, but in many applications binary numbers are difficult to use and a variation of binary numbers is needed. This is where Gray codes are very useful.

Gray code has property that **two successive numbers differ in only one bit** because of this property gray code does the cycling through various states with minimal effort and used in K-maps, error correction, communication etc.

**How to generate n bit Gray Codes?**

Following is 2-bit sequence (n = 2) 00 01 11 10 Following is 3-bit sequence (n = 3) 000 001 011 010 110 111 101 100 And Following is 4-bit sequence (n = 4) 0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000

n-bit Gray Codes can be generated from list of (n-1)-bit Gray codes using following steps.

- Let the list of (n-1)-bit Gray codes be L1. Create another list L2 which is reverse of L1.
- Modify the list L1 by prefixing a ‘0’ in all codes of L1.
- Modify the list L2 by prefixing a ‘1’ in all codes of L2.
- Concatenate L1 and L2. The concatenated list is required list of n-bit Gray codes.
- The Most Significant Bit (MSB) of the gray code is always equal to the MSB of the given binary code.
- Other bits of the output gray code can be obtained by XORing binary code bit at that index and previous index.
- The Most Significant Bit (MSB) of the binary code is always equal to the MSB of the given binary number.
- Other bits of the output binary code can be obtained by checking gray code bit at that index. If current gray code bit is 0, then copy previous binary code bit, else copy invert of previous binary code bit.

Please refer Generate n-bit Gray Codes for detailed program.

**How to Convert Binary To Gray and Vice Versa?**

Binary : 0011 Gray : 0010 Binary : 01001 Gray : 01101

In computer science many a times we need to convert binary code to gray code and vice versa. This conversion can be done by applying following rules :

**Binary to Gray conversion :**

**Gray to binary conversion :**

Below is the implementation of above steps.

## C++

// C++ program for Binary To Gray // and Gray to Binary conversion #include <iostream> using namespace std; // Helper function to xor two characters char xor_c(char a, char b) { return (a == b) ? '0' : '1'; } // Helper function to flip the bit char flip(char c) { return (c == '0') ? '1' : '0'; } // function to convert binary string // to gray string string binarytoGray(string binary) { string gray = ""; // MSB of gray code is same as binary code gray += binary[0]; // Compute remaining bits, next bit is comuted by // doing XOR of previous and current in Binary for (int i = 1; i < binary.length(); i++) { // Concatenate XOR of previous bit // with current bit gray += xor_c(binary[i - 1], binary[i]); } return gray; } // function to convert gray code string // to binary string string graytoBinary(string gray) { string binary = ""; // MSB of binary code is same as gray code binary += gray[0]; // Compute remaining bits for (int i = 1; i < gray.length(); i++) { // If current bit is 0, concatenate // previous bit if (gray[i] == '0') binary += binary[i - 1]; // Else, concatenate invert of // previous bit else binary += flip(binary[i - 1]); } return binary; } // Driver program to test above functions int main() { string binary = "01001"; cout << "Gray code of " << binary << " is " << binarytoGray(binary) << endl; string gray = "01101"; cout << "Binary code of " << gray << " is " << graytoBinary(gray) << endl; return 0; }

## Java

// Java program for Binary To Gray // and Gray to Binary conversion import java.io.*; class code_conversion { // Helper function to xor // two characters char xor_c(char a, char b) { return (a == b) ? '0' : '1'; } // Helper function to flip the bit char flip(char c) { return (c == '0') ? '1' : '0'; } // function to convert binary // string to gray string String binarytoGray(String binary) { String gray = ""; // MSB of gray code is same // as binary code gray += binary.charAt(0); // Compute remaining bits, next bit is // comuted by doing XOR of previous // and current in Binary for (int i = 1; i < binary.length(); i++) { // Concatenate XOR of previous bit // with current bit gray += xor_c(binary.charAt(i - 1), binary.charAt(i)); } return gray; } // function to convert gray code // string to binary string String graytoBinary(String gray) { String binary = ""; // MSB of binary code is same // as gray code binary += gray.charAt(0); // Compute remaining bits for (int i = 1; i < gray.length(); i++) { // If current bit is 0, // concatenate previous bit if (gray.charAt(i) == '0') binary += binary.charAt(i - 1); // Else, concatenate invert of // previous bit else binary += flip(binary.charAt(i - 1)); } return binary; } // Driver program to test above functions public static void main(String args[]) throws IOException { code_conversion ob = new code_conversion(); String binary = "01001"; System.out.println("Gray code of " + binary + " is " + ob.binarytoGray(binary)); String gray = "01101"; System.out.println("Binary code of " + gray + " is " + ob.graytoBinary(gray)); } } // This code is contributed by Anshika Goyal.

## C#

// C# program for Binary To Gray // and Gray to Binary conversion. using System; class GFG { // Helper function to xor // two characters static char xor_c(char a, char b) { return (a == b) ? '0' : '1'; } // Helper function to flip the bit static char flip(char c) { return (c == '0') ? '1' : '0'; } // function to convert binary // string to gray string static String binarytoGray(String binary) { String gray = ""; // MSB of gray code is same // as binary code gray += binary[0]; // Compute remaining bits, next // bit is comuted by doing XOR // of previous and current in // Binary for (int i = 1; i < binary.Length; i++) { // Concatenate XOR of previous // bit with current bit gray += xor_c(binary[i - 1], binary[i]); } return gray; } // function to convert gray code // string to binary string static String graytoBinary(String gray) { String binary = ""; // MSB of binary code is same // as gray code binary += gray[0]; // Compute remaining bits for (int i = 1; i < gray.Length; i++) { // If current bit is 0, // concatenate previous bit if (gray[i] == '0') binary += binary[i - 1]; // Else, concatenate invert of // previous bit else binary += flip(binary[i - 1]); } return binary; } // Driver program to test above // functions public static void Main() { String binary = "01001"; Console.WriteLine("Gray code of " + binary + " is " + binarytoGray(binary)); String gray = "01101"; Console.Write("Binary code of " + gray + " is " + graytoBinary(gray)); } } // This code is contributed by nitin mittal.

Output:

Gray code of 01001 is 01101 Binary code of 01101 is 01001

This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above