Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bitset for more than 32 bits?

Tags:

c++

stl

bitset

I need to use bit flags with more than 32 bits (33 to be exact right now). I tried and find std::bitset doesn't handle more than 32 bits (ulong). Do I have to use vector or there's a way to make bitset to work?

I am limited to c++98 in this project so I can't use boost.

Thanks.

Edit:

I'd like to do something like this:

const uint64    kBigNumber = 1LL << 33;
std::bitset<33> myBitSet;
...
switch(myBitSet) {
    case kBigNumber:
    // do something
    ...
}
like image 742
Stephen Chu Avatar asked Dec 05 '22 01:12

Stephen Chu


1 Answers

std::bitset should work with more or less arbitrary sizes -- it's not normally limited to the size of an unsigned long (though it can look that way, because there's a constructor that builds a bitset based on the bits in an unsigned long).

If that won't work, vector<bool> may be useful for you, though you should be aware that it's pretty much a vector in name only -- it is not really a container (i.e., doesn't conform to the normal container requirements).

like image 163
Jerry Coffin Avatar answered Dec 09 '22 14:12

Jerry Coffin