java.util.BitSet -- set() doesn't work as expected

Posted by dwhsix on Stack Overflow See other posts from Stack Overflow or by dwhsix
Published on 2010-05-18T01:40:12Z Indexed on 2010/05/18 1:50 UTC
Read the original article Hit count: 289

Filed under:
|
|

Am I missing something painfully obvious? Or does just nobody in the world actually use java.util.BitSet?

The following test fails:

@Test
public void testBitSet() throws Exception {
    BitSet b = new BitSet();
    b.set(0, true);
    b.set(1, false);
    assertEquals(2, b.length());
}

It's really unclear to me why I don't end up with a BitSet of length 2 and the value 10. I peeked at the source for java.util.BitSet, and on casual inspection it seems to fail to make sufficient distinction between a bit that's been set false and a bit that has never been set to any value...

(Note that explicitly setting the size of the BitSet in the constructor has no effect, e.g.:

BitSet b = new BitSet(2);

© Stack Overflow or respective owner

Related posts about java

Related posts about bit-manipulation