📜  格雷码的十进制等效及其逆

📅  最后修改于: 2021-05-25 05:01:26             🧑  作者: Mango

给定一个十进制数n。用十进制形式找到该数字的格雷码。

例子 :

下表显示了将二进制代码值转换为格雷代码值的方法:

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

以下是将十进制代码值转换为格雷代码值的方法。
令G(n)为等效于二进制表示n的格雷码。考虑数字n和数字比特G(n)的比特。注意,n和G(n)的最左置位在同一位置。假设此位置为i,其右边的位置为(i + 1),(i + 2_等)。如果n中的第i位为1,则G(n)中的(i +1)位为0,并且反之亦然,第(i + 2)位也是如此,以此类推,因此我们有G(n)= n xor(n >> 1):

C++
// CPP Program to convert given
// decimal number into decimal
// equivalent of its gray code form
#include 
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;
}


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


Python 3
# 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


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


PHP
> 1);
}
  
    // Driver Code
    $n = 10;
    echo grayCode($n) ;
  
// This code is contributed by nitin mittal.
?>


C++
// CPP Program to convert given
// decimal number of gray code
// into its inverse in decimal form
#include 
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;
}


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


Python3
# 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


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
> 1)
        $inv ^= $n;
  
    return $inv;
}
  
    // Driver Code
    $n = 15;
    echo inversegrayCode($n);
  
// This code is contributed by anuj_67.
?>


输出:
15

查找灰色反码
给定格雷码的十进制等效数字n。查找十进制形式的反数字。

例子:

下面是将格雷码值转换为十进制码值的方法。
我们将从较旧的位转到较年轻的位(即使最小的位也具有数字1,而最老的位则被编号为k)。我们获得第n i个数字n的位与第g i个数字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++

// CPP Program to convert given
// decimal number of gray code
// into its inverse in decimal form
#include 
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;
}

Java

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

Python3

# 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

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

> 1)
        $inv ^= $n;
  
    return $inv;
}
  
    // Driver Code
    $n = 15;
    echo inversegrayCode($n);
  
// This code is contributed by anuj_67.
?>
输出:
10