diff --git a/bitset.go b/bitset.go index 2ee2213..5d0cd2e 100644 --- a/bitset.go +++ b/bitset.go @@ -662,16 +662,18 @@ func (b *BitSet) PreviousSet(i uint) (uint, bool) { if x >= len(b.set) { return 0, false } - w := b.set[x] + word := b.set[x] + // Clear the bits above the index - w = w & ((1 << (wordsIndex(i) + 1)) - 1) - if w != 0 { - return uint(x<= 0; x-- { - w = b.set[x] - if w != 0 { - return uint(x<= len(b.set) { return 0, false } - w := b.set[x] + word := b.set[x] + // Flip all bits and find the highest one bit - w = ^w + word = ^word + // Clear the bits above the index - w = w & ((1 << (wordsIndex(i) + 1)) - 1) - if w != 0 { - return uint(x<= 0; x-- { - w = b.set[x] - w = ^w - if w != 0 { - return uint(x<