我有一个给定长度的 C++ 位集。我想生成这个位集的所有可能组合,我想为此添加 1 2^bitset.length 次。这个怎么做? Boost库解决方案也可以接受
最佳答案
试试这个:
/*
* This function adds 1 to the bitset.
*
* Since the bitset does not natively support addition we do it manually.
* If XOR a bit with 1 leaves it as one then we did not overflow so we can break out
* otherwise the bit is zero meaning it was previously one which means we have a bit
* overflow which must be added 1 to the next bit etc.
*/
void increment(boost::dynamic_bitset<>& bitset)
{
for(int loop = 0;loop < bitset.count(); ++loop)
{
if ((bitset[loop] ^= 0x1) == 0x1)
{ break;
}
}
}
关于c++ - 将 1 添加到 C++ 位集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10362991/