c++ - 将 1 添加到 C++ 位集

标签 c++ boost bitset

我有一个给定长度的 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/

相关文章:

c++ - 使用 boost 库检查 CRC 没有给出令人满意的结果

c# - 检索 Windows 8 产品 key

c++ - fscanf 有时会停止工作

c++ - 在 beast 1.70.0 中使用 Tcp 超时

c++ - 序列化其中包含可序列化嵌套类的类,出现奇怪的编译错误

c++ - Boost multi_index unqiue 索引问题

java - 如何在 Java 中连接两个位集?

C++11 vector 的智能指针

c# - 如何创建一个 DirectShow 图形来等待传入的图像并将它们作为帧添加到视频文件中?

c++ - 为什么 std::bitset<8> 变量无法处理 11111111?