views:

56

answers:

2

Hi!

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!

A: 

Ignoring the title of your question, lets answer this question, instead:

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

The best way is to define your vector as vector<unsigned char> (or vector<uint32_t>, or whichever other integer type you choose), and do your bitwise operations how you normally would for an array of unsigned integers. Things will be much faster this way, and there will be no hidden mechanism.

You can use division (or bitwise operators, if you're slick) to resolve which array index you need to operate against, and for-loops to apply bitwise operations larger than a single element.

Here's a related question: http://stackoverflow.com/questions/3956898/bit-twiddling-a-lot-of-bits-in-c/3956920#3956920

You will basically be doing these same operations, if and when you decide to wrap vector<unsigned some-int-type> with your own operators.

Merlyn Morgan-Graham
so if I still want the possibility to access my boolean values directly I actually have to encapsule vector<unsigned char> and do the mechanics vector<bool> does myself, just additionally also providing functionality for bitwise operations?
Mat
@Mat: You don't *have* to do anything, but to get the best performance, I do suggest doing what you just described. Wrap the whole thing in a class, and implement overloaded operators against it. Then you can optimize to your heart's content. It really isn't very complicated code to implement, and gives you the right balance of abstraction and performance.
Merlyn Morgan-Graham
actually i just wanted to know by that whether this functionality doesn't maybe already exist? because it appears logical to me that this is something you want to do with bitvectors
Mat
@Mat: I think I've read that people regard the specialization on `vector<bool>` as having been a bad idea, so the standards committee might have decided not to throw good money after bad. I wouldn't be too surprised if there was a third party library, but there is nothing standard.
Merlyn Morgan-Graham
@Mat: If you can use a third party library, go for ArunSaha's answer :)
Merlyn Morgan-Graham
ok I'll give that a try! thanks! =)
Mat
+2  A: 

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

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

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

ArunSaha
+1 because I'd use these libraries if I decided I could take the dependencies on for the project I was working on.
Merlyn Morgan-Graham