java中二进制字符串的异或

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20745387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:02:55  来源:igfitidea点击:

Xor of binary strings in java

java

提问by Kailash

i am using the following code to XOR 2 strings

我正在使用以下代码对 2 个字符串进行 XOR

String lseq = "0011111111101111111111111100101101111100110000001011111000010100";
String tseq = "0011111111100000110011001100110011001100110011001100110011001100";

StringBuilder sb = new StringBuilder();

for(int i = 0; i < lseq.length(); i++)
    sb.append((lseq.charAt(i) ^ tseq.charAt(i)));

String result = sb.toString();
System.out.println(result);

The above code gives me correct output :

上面的代码给了我正确的输出:

0000000000001111001100110000011110110000000011000111001011011000

I need to XOR one more string

我需要再异或一个字符串

String hseq = "0011111111110010010111110100010111100000101101001110000100011110";

if i try sb.append((lseq.charAt(i) ^ tseq.charAt(i) ^ hseq.charAt(i));

如果我尝试 sb.append((lseq.charAt(i) ^ tseq.charAt(i) ^ hseq.charAt(i));

I am getting result :

我得到结果:

48484848484848484848484948484948494848494848494949484848494848494848494849494848484949494948484848484948494948494949484948484948

which is wrong. I need help in doing xor of 3 binary strings

这是错误的。我需要帮助做 3 个二进制字符串的异或

采纳答案by Elliott Frisch

I would do it like this

我会这样做

private static boolean bitOf(char in) {
    return (in == '1');
}

private static char charOf(boolean in) {
    return (in) ? '1' : '0';
}

public static void main(String[] args) {
    String lseq ="0011111111101111111111111100101101111100110000001011111000010100";
    String tseq ="0011111111100000110011001100110011001100110011001100110011001100";
    String hseq ="0011111111110010010111110100010111100000101101001110000100011110";

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < lseq.length(); i++) {
        sb.append(charOf(bitOf(lseq.charAt(i)) ^ bitOf(tseq.charAt(i))
                ^ bitOf(hseq.charAt(i))));
    }

    String result = sb.toString();
    System.out.println(result);
}

Which outputs

哪些输出

0011111111111101011011000100001001010000101110001001001111000110

回答by Guido

You can use BigInteger, it will make your code simpler. It has a constructor where you can pass a String and the base you want to use (2 in your case).

您可以使用BigInteger,它会使您的代码更简单。它有一个构造函数,您可以在其中传递一个字符串和要使用的基数(在您的情况下为 2)。

Then you can do a XOR calling public BigInteger xor(BigInteger val)(see docsor take a look to openjdk BigInteger's code)

然后您可以进行 XOR 调用public BigInteger xor(BigInteger val)(请参阅文档或查看openjdk BigInteger 的代码

回答by ian

You could also consider something like this:

你也可以考虑这样的事情:

String lseq = "0011111111101111111111111100101101111100110000001011111000010100";
String tseq = "0011111111100000110011001100110011001100110011001100110011001100";
String hseq = "0011111111110010010111110100010111100000101101001110000100011110";

StringBuilder sb = new StringBuilder();

for(int i = 0; i < lseq.length(); i++)
    sb.append((lseq.charAt(i) - '0' ^ tseq.charAt(i) - '0' ^ hseq.charAt(i) - '0'));

String result = sb.toString();
System.out.println(result);

Under the hood, a charis treated like an intin that the numeric value represents a pre-defined character. We can subtract the value of the character '0' from our character (knowing that the value of '1' is only 1 more than '0') and get either 0 or 1, which can be used with the ^operator.

在幕后, achar被视为 an int,因为数值表示预定义的字符。我们可以从我们的字符中减去字符'0'的值(知道'1'的值只比'0'多1)得到0或1,可以和^运算符一起使用。