tyler/bitset

Strange issue with a Bitset of size 64 (cardinality always 0)

Closed this issue · 3 comments

If you have a Bitset of size 64, you get an incorrect cardinality value (always 0).

bits = Bitset.new(64)
bits.set(13,23)
puts bits.cardinality

Results in the value of 0, when it should be 2.

This occurs with all Bitsets that have sizes that are multiples of 64. Bits set in the final word will not be counted.

For example:

a = Bitset.new(128)
a[65] = 1
a.cardinality #= prints 0

Error down to this line:

if(i+1 == max)
        segment &= ((((uint64_t) 1) << (bs->len & 0x3F)) - 1)

Substituting bs->len for any multiple of 64 results in segment &= 0

This is a bit beyond my comfort zone. I'd suggest a modulo check, but the source looks like it's geared for speed.

Perhaps if(i+1 == max && (bs->len & 0x3F)) ?

I'm closing this issue as a great deal of time has passed. If there is renewed interest just reopen or make a new issue and link to this one.