diff --git a/bitset.go b/bitset.go index 7f01221..fbf0496 100644 --- a/bitset.go +++ b/bitset.go @@ -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 } diff --git a/bitset_test.go b/bitset_test.go index 4720f2a..efa202f 100644 --- a/bitset_test.go +++ b/bitset_test.go @@ -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) {