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);
Good Casper! Your small improvement should indeed have been present in the original BitSet java def! I also suggest this (append() and concat() are useful for various usages)
This puzzled me too, not sure of the rationale behind BitSet's current rather unexpected functionality. However since it's not final, we can use some embrace and extend tactics and do the following to get a fixed BitSet with length semantics as expected:
You highest bit set (as in "set to 1") is Bit 0. So the length should be 1.
See the JavaDoc for length:
Maybe you're looking for size although it's possible that might be higher than two if bits are allocated at a certain resolution (say 16 bit boundaries)?
// Abhay Dandekar
A simple java program to show what happens inside. Some points to note :
BitSet is backed by a long
All the default values are false
While returning the length, it returns the index+1 of the highest "true" value in the set.
The output below should be able to explain itself :
So points to conclude :
Do not use the length to conclude the no of bits modified
Can be used in scenarios like bloom filters. More on bloom filters can be googled .. ;)
Hope this helps
Regards,
Abhay Dandekar
People do use
BitSet
; however, they use it for something other than what you intend. It's probably best to think ofBitSet
as a very compact, memory-efficient form ofSet<Integer>
that has the peculiar property that you can't put negative numbers into it.It's very common with
BitSet
s to use them in the pattern ofafter you do something to fill them up. This is equivalent to iterating over the elements of the
Set
.Given that the bitset is backed by a long[], the minimum size is 64 (because 1 long is 64 bits). The size gets incremented by a multiple of 64 and for some reason, they have not maintained the # of bits you intended to represent when you use the constructor that takes an int.