📜  不使用递归将二进制码转换为格雷码的Java程序

📅  最后修改于: 2022-05-13 01:55:28.871000             🧑  作者: Mango

不使用递归将二进制码转换为格雷码的Java程序

数字的二进制代码是二进制(基数 2)数字系统中数字的表示。在二进制数字系统中,每个数字仅使用两个字面量(0 和 1)表示。这些字面量中的每一个都称为bit 。二进制数系统在数字电子电路中非常有用。

数字的格雷码是使用二进制字面量表示的数字。但二进制码与格雷码的区别在于,在格雷码中,每对连续数字仅相差一位。格雷码对于实现 K-Map 和纠错非常有用。

例子:

Binary -> 0100
Gray   -> 0110
   
Binary -> 0101
Gray   -> 0111

格雷码可以转换为二进制码,反之亦然。

二进制到灰色转换:

二进制码可以转换为等效的格雷码,如下所示:

  • 二进制代码的MSB(最高有效位)将是等效格雷码的 MSB。
  • 所有剩余的位都是通过对该位置的位与二进制字符串中前一位置的位执行异或运算获得的。
Example:
Binary ->  0011010101
           0 XOR 0 XOR 1 XOR 1 XOR 0 XOR 1 XOR 0 XOR 1 XOR 0 XOR 1
           ↓  ↓     ↓     ↓     ↓     ↓     ↓     ↓     ↓     ↓
Gray   ->  0  0     1     0     1     1     1     1     1     1
   
   
Binary ->  0100110
Gray   ->  0110101

代码 1:

我们使用'^'通过使用Integer.parseInt()函数将字符串转换为整数值,然后对它们执行异或来执行异或运算。

Java
// Java program to demonstrate Binary
// to Gray conversion
 
import java.io.*;
 
class GFG {
 
    // converts the given binary string into its equivalent
    // gray code
    public static void toGray(String binary)
    {
        // a String variable to store the obtained gray
        // string.
        // the MSB of the gray code is the same as
        // the MSB of the binary string, i.e., binary[0]
        String gray = new String();
 
        gray += binary.charAt(0);
 
        
        for (int i = 1; i < binary.length(); i++)
        {
            // perform XOR on the previous bit and the
            // current bit of the binary string
         gray += (Integer.parseInt(String.valueOf(binary.charAt(i - 1))) ^
                   Integer.parseInt(String.valueOf(binary.charAt(i))));
           
        }
 
        System.out.println("The gray code of " + binary
                           + " is : " + gray);
    }
 
     
    public static void main(String[] args)
    {
        // a String variable to store the given binary
        // string
        String binary = "0011010101";
 
        toGray(binary);
    }
}


Java
// Java program to demonstrate Binary
// to Gray conversion
 
import java.io.*;
 
class GFG {
   
    // an auxiliary method to perform XOR on two given
    // literals
    public static int xOR(char a, char b)
    {
        // return 1 if both bits are not same
        if (a != b)
            return 1;
       
        // else return 0
        return 0;
    }
   
    // converts the given binary string into its equivalent
    // gray code
    public static void toGray(String binary)
    {
       
        String gray = new String();
       
        gray += binary.charAt(0);
       
        // for all the other bits, traverse through the
        // binary string
        for (int i = 1; i < binary.length(); i++)
        {
            // calling xOR() method on the previous bit and
            // the current bit of the binary string
            gray += xOR(binary.charAt(i - 1),
                        binary.charAt(i));
        }
       
        System.out.println("The gray code of " + binary
                           + " is : " + gray);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        // a String variable to store the given binary
        // string
        String binary = "0011010101";
       
        toGray(binary);
       
    }
}


输出
The gray code of 0011010101 is : 0010111111

代码 2:

我们创建了一个名为xOR(char a, char b)的单独的用户定义函数,它返回两个数字 a 和 b 的异或。

Java

// Java program to demonstrate Binary
// to Gray conversion
 
import java.io.*;
 
class GFG {
   
    // an auxiliary method to perform XOR on two given
    // literals
    public static int xOR(char a, char b)
    {
        // return 1 if both bits are not same
        if (a != b)
            return 1;
       
        // else return 0
        return 0;
    }
   
    // converts the given binary string into its equivalent
    // gray code
    public static void toGray(String binary)
    {
       
        String gray = new String();
       
        gray += binary.charAt(0);
       
        // for all the other bits, traverse through the
        // binary string
        for (int i = 1; i < binary.length(); i++)
        {
            // calling xOR() method on the previous bit and
            // the current bit of the binary string
            gray += xOR(binary.charAt(i - 1),
                        binary.charAt(i));
        }
       
        System.out.println("The gray code of " + binary
                           + " is : " + gray);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        // a String variable to store the given binary
        // string
        String binary = "0011010101";
       
        toGray(binary);
       
    }
}
输出
The gray code of 0011010101 is : 0010111111