Skip to content

Commit

Permalink
Fix Shrink not clearing unused bits
Browse files Browse the repository at this point in the history
when new length requires multiple words
  • Loading branch information
omerfirmak committed Nov 18, 2022
1 parent 6752638 commit fd65337
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ func (b *BitSet) Shrink(lastbitindex uint) *BitSet {
copy(shrunk, b.set[:idx])
b.set = shrunk
b.length = length
if length < 64 {
b.set[idx-1] &= allBits >> uint64(64-wordsIndex(length))
lastWordUsedBits := length % 64
if lastWordUsedBits != 0 {
b.set[idx-1] &= allBits >> uint64(64-wordsIndex(lastWordUsedBits))
}
return b
}
Expand Down
9 changes: 9 additions & 0 deletions bitset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,15 @@ func TestShrink(t *testing.T) {
t.Error("24 should be set")
return
}

b = New(110)
b.Set(80)
b.Shrink(70)
for _, word := range b.set {
if (word != 0) {
t.Error("word should be 0", word)
}
}
}

func TestInsertAtWithSet(t *testing.T) {
Expand Down

0 comments on commit fd65337

Please sign in to comment.