Decimal Equivalent of Gray Code and its Inverse
Given a decimal number n. Find the gray code of this number in decimal form.
Examples:
Input : 7
Output : 4
Explanation: 7 is represented as 111 in binary form. The equivalent gray code
of 111 is 100 in binary form, whose decimal equivalent is 4.Input : 10
Output : 15
Explanation: 10 is represented as 1010 in binary form. The equivalent gray code
of 1010 is 1111 in binary form, whose decimal equivalent is 15.
The following table shows the conversion of binary code values to gray code values:
Decimal Value | Binary Equivalent | Gray Code Equivalent | Decimal Value of Gray Code Equivalent |
---|---|---|---|
0 | 000 | 000 | 0 |
1 | 001 | 001 | 1 |
2 | 010 | 011 | 3 |
3 | 011 | 010 | 2 |
4 | 100 | 110 | 6 |
5 | 101 | 111 | 7 |
6 | 110 | 101 | 5 |
7 | 111 | 100 | 4 |
Below is the approach for the conversion of decimal code values to gray code values.
Let G(n) be the Gray code equivalent of binary representation n. Consider bits of a number n and a number bit G(n). Note that the leftmost set bits of both n and G(n) are in the same position. Let this position be i and positions on the right of it be (i+1), (i+2), etc. The (i + 1)th bit in G(n) is 0 if (i+1)th bit in n is 1 and vice versa is also true. The same is true for (i+2)-th bits, etc. Thus we have G (n) = n xor (n >> 1):
C++
Java
Python3
C#
PHP
Javascript
15
Time Complexity: O(1)
Auxiliary Space: O(1)
Finding the Inverse Gray Code
Given a decimal equivalent number n of a gray code. Find its original number in decimal form.
Examples:
Input : 4
Output : 7Input : 15
Output : 10
Below is the approach for the conversion of gray code values to decimal code values.
We will go from the older bits to the younger ones (even the smallest bit has the number 1, and the oldest bit is numbered k). We obtain such relations between the bits of the ni number n and the bits of the gi number g:
Steps to solve this problem:
- Initialize a variable inv to 0.
- Iterate over the binary digits of n from the rightmost digit to the leftmost digit and right shift n by one bit.
- Take the bitwise XOR of the current value of inv with the value of n.
- return the value of inv.
nk = gk, nk-1 = gk-1 xor nk = gk xor gk-1 nk-2 = gk-2 xor nk-1 = gk xor gk-1 xor gk-2 nk-3 = gk-3 xor nk-2 = gk xor gk-1 xor gk-2 xor gk-3 ...
C++
Java
Python3
C#
// C# Program to convert given // decimal number of gray code // into its inverse in decimal form using System; class GFG { // Function to convert given // decimal number of gray code // into its inverse in decimal form static int inversegrayCode( int n) { int inv = 0; // Taking xor until n becomes zero for ( ; n != 0 ; n = n >> 1) inv ^= n; return inv; } // Driver code public static void Main () { int n = 15; Console.Write(inversegrayCode(n)); } } // This code is contributed by nitin mittal. |
PHP
Javascript
10
Time Complexity: O(log n)
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.