Decimal Equivalent of Gray Code and Its Inverse


Gray code or reflected binary code is a form of a binary representation of numbers in which two consecutive numbers only differ by one bit.

For example, the gray code of 1 is 001, while the gray code of 2 is 011.

Gray code is usually used in error correction because it prevents some data errors that can happen in the usual binary representations while state changes.

Gray code is also helpful in k-maps, communication, etc., because of its unique property.

Prerequisite

Study decimal, binary and gray code notations before reading further.

Problem Statement 1

Given a decimal number n, find the gray code of the number in decimal form.

Examples

Input: 3
Output: 2

Explanation -> The binary representation of 3 is 011. Its gray code representation is 010. The decimal equivalent of 010 is 2.

Hence, the decimal equivalent of the gray code of 3 is 2.

Input: 5
Output: 7

Explanation -> The binary representation of 5 is 101. Its gray code representation is 111, and its decimal equivalent is 7.

Hence, the decimal equivalent of the gray code of 5 is 7.

Solution

The compiler understands the numbers in binary format.

Thus, in our program, when we input a number in decimal format, it is interpreted in binary.

Hence, we only need to convert a number from its binary equivalent to its gray code.

Binary to Gray Code Conversion

The leftmost bits of the binary representation and the gray code is equal. The following bits to the right can be found by taking xor of successive binary bits.

For example −

Consider n = 3. The binary code of 3 is 011.

  • The leftmost bit of binary and gray code is equal. Hence, the first bit from the left in the gray code is 0.

  • For the second bit from the left, xor the first and second bit from the left in the binary code. 0 XOR 1 = 1.

  • For the third bit from the left, xor the second and third bit from the left in the binary code. 1 XOR 1 = 0.

Thus the gray code: 010.

Algorithm: Using Bitwise Operators

We can obtain the gray code of a number n by the following steps −

  • Right shift n by 1.

  • Xor the right shifted number with original n.

Example

Below is a C++ program for finding gray code from binary code using bitwise operators

#include <bits/stdc++.h>
using namespace std;
//This function returns the decimal equivalent
// of the gray code of n.
int dec_equi_of_gray(int n) {
   return n ^ (n >> 1);
}
int main(){
   int n = 3;
   cout<<"The decimal equivalent of the gray code of 3 is: ";
   
   //Function call to convert binary code to gray code
   cout << dec_equi_of_gray(n) << endl;
   return 0;
}

Output

The decimal equivalent of the gray code of 3 is: 2

Problem Statement 2

Given the decimal value of the gray code, find its decimal code value.

Examples

Input: 15
Output: 10

Explanation -> The gray code given as input : 1111 (binary value of 15).

Now, converting gray code to binary code gives 1010 from 1111.

And 1010 is the binary value of 10. Hence, the output.

Input: 10
Output: 12

Explanation -> The gray code given as input: 1010 (binary value of 10).

The binary code of gray code 1010 is 1100. And 1100 is 12 in decimals.

Gray code to Binary Code Conversion

The leftmost bit (MSB) of the binary code is the same as the MSB of the gray code. The following bits can be found by taking xor of the previous index binary bit and the current index gray bit.

For example: Consider the gray code 1111.

  • The MSB of the binary code will be the same as the MSB of gray code. Hence, the MSB will be 1.

  • For the second leftmost bit, check the xor of the second leftmost bit of gray code and the leftmost bit of binary code. Hence, 1 ^ 1 = 0.

  • Similarly, for the third leftmost bit, 0 ^ 1 = 1.

  • For the fourth leftmost bit, 1 ^ 1 = 0.

Hence the binary code: 1010.

Example

Below is a C++ program for finding binary code from gray code using bitwise operators

#include <bits/stdc++.h>
using namespace std;

//This function returns the decimal value of 
//the binary code converted from the gray code n.
int gray_to_binary(int n){
   int binary = n;
   while (n > 0){
      n >>= 1;
      binary ^= n;
   }
   return binary;
}
// Driver Code
int main(){
   int n = 15;
   cout<<"The decimal value of the binary code converted from the gray code is: ";
   
   // Function call to convert gray code to binary code
   cout << gray_to_binary(n) << endl;
   
   return 0;
}

Output

The decimal value of the binary code converted from the gray code is: 10

Conclusion

This article solved the problem of finding the Decimal Equivalent of Gray Code and its Inverse for a given number n. We solved the problem using bitwise operators. C++ programs are provided for both of the parts of the problem.

Updated on: 10-Mar-2023

746 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements