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 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 gray code
of 1010 is 1111 in binary form whose decimal equivalent is 15

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
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 Gray code equivalent of binary represention n. Consider bits of a number n and a number bit G(n). Note that leftmost set bits of both n and G(n) are at same position. Let this position be i and positions on right of it be (i+1), (i+2_, etc. The (i + 1)th bit in G(n) is 0 if i-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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to convert given
// decimal number into decimal
// equivalent of its gray code form
#include <bits/stdc++.h>
using namespace std;
  
int grayCode(int n)
{
    /* Right Shift the number by 1
       taking xor with original number */
    return n ^ (n >> 1);
}
  
// Driver Code
int main()
{
    int n = 10;
    cout << grayCode(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert given
// decimal number into decimal
// equivalent of its gray code form
class GFG {
      
    static int grayCode(int n)
    {
          
        // Right Shift the number 
        // by 1 taking xor with 
        // original number 
        return n ^ (n >> 1);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
          
        int n = 10;
          
        System.out.println(grayCode(n));
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to convert
# given decimal number into 
# decimal equivalent of its 
# gray code form
  
def grayCode(n):
  
    # Right Shift the number
    # by 1 taking xor with 
    # original number
    return n ^ (n >> 1)
  
  
# Driver Code
n = 10
print(grayCode(n))
  
# This code is contributed
# by Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to convert given
// decimal number into decimal
// equivalent of its gray code form
using System;
  
public class GFG {
      
    // Function for conversion
    public static int grayCode(int n)
    {
          
        // Right Shift the number 
        // by 1 taking xor with 
        // original number 
        return n ^ (n >> 1);
    }
      
    // Driver Code
    static public void Main ()
    {
        int n = 10;
          
        Console.WriteLine(grayCode(n));
    }
}
  
// This code is contributed by Ajit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to convert given
// decimal number into decimal
// equivalent of its gray code form
  
function grayCode($n)
{
    /* Right Shift the number by 1
       taking xor with original
       number */
    return $n ^ ($n >> 1);
}
  
    // Driver Code
    $n = 10;
    echo grayCode($n) ;
  
// This code is contributed by nitin mittal.
?>

chevron_right


Output:

15

Finding the Gray Inverse Code
Given the decimal equivalent number n of a gray code. Find the inverse number in decimal form.

Examples:

Input : 4
Output : 7

Input : 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 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:

 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to convert given
// decimal number of gray code
// into its inverse in decimal form
#include <bits/stdc++.h>
using namespace std;
  
int inversegrayCode(int n)
{
    int inv = 0;
  
    // Taking xor until n becomes zero
    for (; n; n = n >> 1)
        inv ^= n;
  
    return inv;
}
  
// Driver Code
int main()
{
    int n = 15;
    cout << inversegrayCode(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert given
// decimal number of gray code
// into its inverse in decimal form
import java.io.*;
  
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 (String[] args)
    {
        int n = 15;
        System.out.println(inversegrayCode(n));
    }
}
  
// This code is contributed by Ajit.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to convert
# given decimal number of 
# gray code into its inverse 
# in decimal form
  
def inversegrayCode(n):
    inv = 0;
      
    # Taking xor until
    # n becomes zero
    while(n):
        inv = inv ^ n;
        n = n >> 1;
    return inv;
  
# Driver Code
n = 15;
print(inversegrayCode(n));
  
# This code is contributed
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to convert given
// decimal number of gray code
// into its inverse in decimal form
  
function inversegrayCode( $n)
{
    $inv = 0;
  
    // Taking xor until
    // n becomes zero
    for (; $n; $n = $n >> 1)
        $inv ^= $n;
  
    return $inv;
}
  
    // Driver Code
    $n = 15;
    echo inversegrayCode($n);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output:

10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.