当我阅读Java中的BitSet类时,遇到了以下示例。是SetSet类的逻辑和,或&xor与逻辑门逻辑相同?

有人可以在上面的示例中解释我的工作方式或方法吗?因为当我阅读有关或方法的oracle文档时,它说:

公共无效或(BitSet设置)

使用bit set参数对该位设置执行逻辑或。修改此位集,以便仅当它已经具有值true或位集参数中的相应位具有值true时,其中的位才具有true值。

参数:set-一点设置

以下是代码::

import java.util.BitSet;

public class BitSetDemo {

  public static void main(String args[]) {
     BitSet bits1 = new BitSet(16);
     BitSet bits2 = new BitSet(16);

     // set some bits
     for(int i=0; i<16; i++) {
        if((i%2) == 0) bits1.set(i);
        if((i%5) != 0) bits2.set(i);
     }
     System.out.println("Initial pattern in bits1: ");
     System.out.println(bits1);
     System.out.println("\nInitial pattern in bits2: ");
     System.out.println(bits2);

     // AND bits
     bits2.and(bits1);
     System.out.println("\nbits2 AND bits1: ");
     System.out.println(bits2);

     // OR bits
     bits2.or(bits1);
     System.out.println("\nbits2 OR bits1: ");
     System.out.println(bits2);

     // XOR bits
     bits2.xor(bits1);e
     System.out.println("\nbits2 XOR bits1: ");
     System.out.println(bits2);
  }
}

The Output of above program is

Initial pattern in bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

Initial pattern in bits2:
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}

bits2 AND bits1:
{2, 4, 6, 8, 12, 14}

bits2 OR bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

bits2 XOR bits1:
{}

so in the above example result of or should be {0,1,2,3,4,6,7,8,9,10,11,12,13,14} according to oracle docs instead of {0, 2, 4, 6, 8, 10, 12, 14}. or am I misunderstood the explanation?

Really appreciate the help.

最佳答案

问题是您使用已从bits2.or(bits1)调用修改的bits2值计算and。与其直接在or上调用andxorbits2,不如对每个测试尝试使用单独的bits2副本。

关于java - Java中的BitSet类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21036453/

10-10 17:24