Home >Backend Development >C++ >Decimal equivalent of Gray code and its reverse order

Decimal equivalent of Gray code and its reverse order

WBOY
WBOYforward
2023-09-07 18:33:021518browse

Decimal equivalent of Gray code and its reverse order

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

For example, the Gray code of 1 is 001, and the Gray code of 2 is 011.

Gray code is often used for error correction because it prevents some data errors that can occur in the usual binary representation when state changes.

Due to its unique properties, Gray code is also helpful in k-map, communication, etc.

prerequisites

Before reading further, please study decimal, binary and Gray code notation.

Problem Statement 1

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

Example

Input: 3
Output: 2

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

Thus, the Gray code decimal equivalent 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 representation is 7.

Thus, the Gray code decimal equivalent of 5 is 7.

solution

The compiler understands numbers in binary format.

So, in our program, when we enter a number in decimal format, it is interpreted as binary.

So we just need to convert the number from its binary equivalent to its Gray code.

Binary to Gray code conversion

The binary representation is equal to the leftmost bit of the Gray code. The following bits on the right side are found by XORing consecutive binary bits.

For example -

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

  • The leftmost bits of binary code and Gray code are equal. Therefore, the first bit from the left in Gray code is 0.

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

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

So Gray code: 010.

Algorithm: Using bitwise operators

We can obtain the Gray code of number n through the following steps -

  • n Shift right by 1.

  • XOR the right-shifted number with the original n.

Example

The following is a C program that uses bitwise operators to find Gray code from binary code

#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 Gray code, find its decimal code value.

Example

Input: 15
Output: 10

Explanation -> Gray code given as input: 1111 (binary value 15).

Now, convert the Gray code to binary code to get 1010 from 1111.

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

Input: 10
Output: 12

Explanation -> Gray code given as input: 1010 (binary value 10).

The binary system of Gray code 1010 is 1100. The decimal system of 1100 is 12.

Conversion from Gray code to binary code

The leftmost bit (MSB) of the binary code is the same as the MSB of the Gray code. The following bits are found by XORing the previous indexed binary bit with the current indexed grayscale bit.

Example: Consider Gray code 1111.

  • The MSB of the binary code will be the same as the MSB of the Gray code. Therefore, the MSB will be 1.

  • For the left two bits, check the XOR of the left two bits of the Gray code and the leftmost bit of the binary code. Therefore, 1^1 = 0.

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

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

So binary code: 1010.

Example

Below is a C program to find 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

in conclusion

This article solves the problem of finding the Gray code decimal equivalent and its inverse of a given number n. We solved this problem using bitwise operators. C programs are provided for both parts of the problem.

The above is the detailed content of Decimal equivalent of Gray code and its reverse order. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete