问题描述
我正在寻找一个好的Java BitSet
示例来处理0和1。我试着查看Javadocs,但是我只是通过阅读来了解该类的用法。例如,和
,或
和 xor 方法适用于两个不同的
BitSet
对象?
I'm looking for a good Java
BitSet
example to work with 0 and 1s. I tried looking at the Javadocs but I don't understand the usage of the class by just reading that. For instance, how would the and
, or
, and xor
methods work on two different BitSet
objects?
例如:
BitSet bits1 = new BitSet();
BitSet bits2 = new BitSet();
bits2.set(1000001);
bits1.set(1111111);
bits2.and(bits1);
System.out.println(bits2);
如果我这样做,它会返回
bits2
as为什么会这样?
If I do this it returns
bits2
as empty why is that?
推荐答案
对于您提到的具体问题:当您调用
bits2.set( 1000001)
,您将百万分之一和第一位设置为true。然后当你与 bits1
相交时,它们设置了一百万,一亿一千一百一十一和第一百一十二位,它们没有共同点。
For the specific problem you mentioned: when you called
bits2.set(1000001)
, you set the one millionth and first bit to true. Then when you intersected with bits1
, which had the one million, 111 thousand, and 111st bit set, they had no bits in common.
我认为你的意思
I think what you meant to do was
bits2.set(0); // set the 0th bit
bits2.set(6); // set the 6th bit
这有助于解决问题吗?
这篇关于Java BitSet示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!