Search code examples
c++bitwise-operatorsbitvector

bitwise operations on vector<bool>


what's the best way to perform bitwise operations on vector<bool>?

as i understand, vector<bool> is a specialisation that uses one bit per boolean. I chose vector<bool> for memory saving reasons. I know that there are some problems with vector<bool> but for my needs it's apropriate.

now - what's the most performant way of aplying bitwise operations to whole such vectors?

if i do it in a for loop and readout each single bool and store it back, the way I understand it a lot more operations are performed inside in order to access the actual values.

thanks!


Solution

  • If the number of bits are fixed at compile time, you would be much better off using std::bitset

    If not, (i.e. number of bits varies at runtime), then you should see and can use boost::dynamic_bitset)

    In both of these, it is extremely easy to do all the bitwise operations.