I need an efficient method with low complexity to change the values of a range of bits in the bitset :
bitset<1000000> bs;
and I need to set values from 100 to 500 to TRUE for example .
what is the fastest method to accomplish this in the lowest complexity ?
using a loop wouldn't suffice for sure .
An ordinary for-loop is the easiest and fastest way:
std::bitset<1000000> bs;
for (unsigned i = start, stop = start + n; i != stop; ++i)
bs.set(i);
Unfortunately:
- given the size of
bs
, working with masks / shifting isn't an option (e.g. see in bitset, can i use "to_ulong" for a specific range of bits?)
- the interface of
bitset
doesn't support ranges / sub-blocks (e.g. What is the performance of std::bitset?). The same limitation is present in boost::dynamic_bitset.
Also consider that std::bitset
doesn't come with iterators, so it cannot be used with the functions in <algorithm>
library.
If performance really matters you could consider an ad-hoc implementation.