Reputation: 67
I have a bitset of size 81 so it doesn't fit into any primitive c++ type. I need to access 9 bit contiguous subsets from the bitset and imagined that I could just shift the big set and call .to_ulong() to extract them. It seems that the STL wants to be safe and just gives me an overflow_error when I try.
int offset = something_that_is < 81;
bitset<81> set;
int subset = ((set >> offset).to_ulong()) & 511;
This snippet, although mathematically perfectly safe, throws an exception. Can I force it got give me the least significant ulong somehow or would I need to extract the bits using the [] operator?
I noticed that there is a underlying implementation consisting of an array but all members that refer to it seem to be private.
while the previous code doesn't work, this one does.
int offset = something_that_is < 81;
bitset<81> set;
int subset = ((set >> offset) & 511).to_ulong();
Although that should probably have been an unsigned long instead of an int.
Upvotes: 2
Views: 385
Reputation: 104569
closer to what I think you want:
std::bitset<81> bs;
const std::bitset<81> nineset = 0x1ff;
int number_of_sets = 81 / 9;
for (int x = 0; x < number_of_sets; x++)
{
std::bitset<81> tmp = bs;
tmp &= nineset;
unsigned long value = tmp.to_ulong(); // this is your value
// prep for the next set
bs >>= 9;
}
Upvotes: 2