From a1cbd25ceaff1ab298754d3855009d2d0c6980c5 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 4 Mar 2022 17:04:56 -0500 Subject: [PATCH] Verifying and checking issue 92. --- bitset.go | 4 +++- bitset_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/bitset.go b/bitset.go index 8eff273..d60eaf2 100644 --- a/bitset.go +++ b/bitset.go @@ -226,7 +226,9 @@ func (b *BitSet) FlipRange(start, end uint) *BitSet { for i := startWord; i < endWord; i++ { b.set[i] = ^b.set[i] } - b.set[endWord] ^= ^uint64(0) >> (-end & (wordSize - 1)) + if end & (wordSize - 1) != 0 { + b.set[endWord] ^= ^uint64(0) >> (-end & (wordSize - 1)) + } return b } diff --git a/bitset_test.go b/bitset_test.go index 7df653f..a4abf72 100644 --- a/bitset_test.go +++ b/bitset_test.go @@ -1409,6 +1409,18 @@ func TestFlipRange(t *testing.T) { t.Error("Unexpected value: ", d.length) return } + // + for i := uint(0); i < 256; i++ { + for j := uint(0); j <= i; j++ { + bits := New(i) + bits.FlipRange(0, j) + c := bits.Count() + if c != j { + t.Error("Unexpected value: ", c, " expected: ", j) + return + } + } + } } func TestCopy(t *testing.T) {