What is the fastest way to clear every kth bit in a boost::dynamic_bitset, optionally from offset j?
Currently I’m doing this which is pretty darn slow (pseudocode):
for (i = j; i < bitset.size(); i += k) {
bitset[i] = 0;
}
Millions of bit-clears have to be done, so that’s why I’m looking for a fast way to do it.
For very large bitsets, compute a mask n bits long (where n is your native size, e.g. 64 for x86_64) as Nim suggested, apply it.
If your native length is not a multiple of k, shift it accordingly.
So if you have a native length of 10 and want to set only every 3rd bit of a 30 bit-long bitset, you’d need 3 passes like this:
First 10 bits: 0010010010
Second 10 bits: 0100100100
Last 10 bits: 1001001001
So, after applying each mask you’d need to shift it (n%k) bits to the left.
Repeat until you’re done.