bits-and-blooms / bitset

Go package implementing bitsets
BSD 3-Clause "New" or "Revised" License
1.34k stars 175 forks source link

Can offer a range Flip method? #87

Closed Tony-Kwan closed 3 years ago

Tony-Kwan commented 3 years ago

Can offer a range Flip method? Flip(start, end uint) flip bit in [start, end)

lemire commented 3 years ago

Yes. It should look something like this...

func flipBitmapRange(bitmap []uint64, start int, end int) {
    if start >= end {
        return
    }
    firstword := start / 64
    endword := (end - 1) / 64
    bitmap[firstword] ^= ^(^uint64(0) << uint(start%64))
    for i := firstword; i < endword; i++ {
        bitmap[i] = ^bitmap[i]
    }
    bitmap[endword] ^= ^uint64(0) >> (uint(-end) % 64)
}

Do you want to write a pull request ?

Tony-Kwan commented 3 years ago

Thx. I will write a pr.

lemire commented 3 years ago

Fixed. Closing.