Skip to content

Commit

Permalink
Merge pull request #196 from gaissmai/fb-top
Browse files Browse the repository at this point in the history
simplify code for top()
  • Loading branch information
lemire authored Dec 22, 2024
2 parents cc750a0 + e474038 commit 5bdbff1
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,18 +1418,13 @@ func (b *BitSet) Select(index uint) uint {

// top detects the top bit set
func (b *BitSet) top() (uint, bool) {
panicIfNull(b)

idx := len(b.set) - 1
for ; idx >= 0 && b.set[idx] == 0; idx-- {
}

// no set bits
if idx < 0 {
return 0, false
for idx := len(b.set) - 1; idx >= 0; idx-- {
if word := b.set[idx]; word != 0 {
return uint(idx<<log2WordSize+bits.Len64(word)) - 1, true
}
}

return uint(idx*wordSize+bits.Len64(b.set[idx])) - 1, true
return 0, false
}

// ShiftLeft shifts the bitset like << operation would do.
Expand Down

0 comments on commit 5bdbff1

Please sign in to comment.