diff --git a/bitset.go b/bitset.go index d688806..271ccdb 100644 --- a/bitset.go +++ b/bitset.go @@ -254,7 +254,9 @@ func (b *BitSet) Shrink(lastbitindex uint) *BitSet { copy(shrunk, b.set[:idx]) b.set = shrunk b.length = length - b.set[idx-1] &= (allBits >> (uint64(64) - uint64(length&(wordSize-1)))) + if length < 64 { + b.set[idx-1] &= (allBits >> (uint64(64) - uint64(length&(wordSize-1)))) + } return b } diff --git a/bitset_test.go b/bitset_test.go index f386d4e..7346b44 100644 --- a/bitset_test.go +++ b/bitset_test.go @@ -564,6 +564,13 @@ func TestAll(t *testing.T) { } func TestShrink(t *testing.T) { + bs := New(10) + bs.Set(0) + bs.Shrink(63) + if !bs.Test(0) { + t.Error("0 should be set") + return + } b := New(0) b.Set(0)