2

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 .

manlio
  • 18,345
  • 14
  • 76
  • 126
Ebram Shehata
  • 446
  • 5
  • 20
  • 1
    I'm voting to close this question as off-topic because SO is not a code writing service, please show your efforts – EdChum Jun 15 '16 at 08:19
  • But I am NOT asking for a code write !? – Ebram Shehata Jun 15 '16 at 08:21
  • @EbramShehata probably you could provide several attempts and see which is more efficient. – Mine Jun 15 '16 at 08:33
  • 5
    `std::bitset` does not allow direct access to the stored data, but if you can implement your own bitset class, say using the bits in `uint64` variables, you could set 64 bits in one instruction. – Karsten Koop Jun 15 '16 at 09:42
  • 1
    note that `bitset<1000000> bs;` has a high chance of causing a stack overflow. I suggest using `auto bs = std::make_unique();` if that troubles you enough. – rubenvb Jun 15 '16 at 10:12

1 Answers1

1

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:

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.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126