为什么java中的BitSet异或值是空的?

Why XOR value is empty in BitSet in java?

我刚刚进入 Advance Java。我不知道为什么 xor() 函数 returning 空集。 据我所知,异或 return 两个相同位为零,两个不同位为 1。所以,如果我从 Bits OneBits two 分别异或前两位,即 0 和 1,为什么 return空集。请详细说明,有必要and/or

代码

public class LearnBitSet {
public static void main(String[] args) {
BitSet bits1 = new BitSet(32);
BitSet bits2 = new BitSet(32);
for (int bitCount = 0; bitCount < 32; bitCount++) {
        if (bitCount % 2 == 0){
            bits1.set(bitCount);
        }
        if (bitCount % 5 != 0){
            bits2.set(bitCount);
        }
    }
System.out.println("Bits One = " + bits1);
System.out.println("Bits Two = " + bits2);
// AND
bits1.and(bits2);
System.out.println("ADD = " +bits1);

// OR
bits1.or(bits2);
System.out.println("OR = "+bits1);
// XOR

bits1.xor(bits2);
System.out.println("XOR = "+bits1);
}

}

输出

Bits One = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30}
Bits Two = {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 31}
ADD = {2, 4, 6, 8, 12, 14, 16, 18, 22, 24, 26, 28}
OR = {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 31}
XOR = {}

编辑第一个 此外,还有基数为 0 的问题。为什么?在输出中,有很多但 bits1.cardinality() return 为零。网上有没有了解BitSet的详细资源

你可以看到bits1的内容和bits2做完OR运算后完全一样:

...
Bits Two = {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 31}
...
OR = {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 31}

因此,如果您在两个操作数相同的情况下执行 XOR 运算,根据定义,您将得到“0”或“False”(参见 truth table),或者在这种情况下 BitSet 所有位设置为 false.

最后根据toString()documentation打印了一个空的BitSet{}