为什么按位 OR 和 AND 逻辑没有按预期工作?

Why bitwise OR and AND logic is not working as expected?

我有下面的示例代码。我一直认为,对枚举值执行按位或运算可以让我检查结果(使用按位与运算)以查看结果中包含哪些枚举值,哪些不包含。

例如,如果我做 result = mouse | headphone,那么我可以检查 agaist result & mouse == mouse 作为条件,以了解 mouse 是否包含在 result 中。但似乎无论我 & 结果如何,比如 X,我总是以 X 结束。为什么?

在下面的代码中,我认为 if 应该失败,因为 straw 没有包含在 options 中,但它没有..

#include <iostream>
#include <iomanip>

using namespace std;

enum Stock  
{
    milk,
    choclate,
    tv,
    cable,
    mouse,
    fan,
    headphone,
    cup,
    straw,
    pen,
    candy,
    glasses,
    book,
    plug

};


int main()
{
    Stock options = static_cast<Stock>( static_cast<int>(mouse) | static_cast<int>(headphone)
                                            | static_cast<int>(cup) | static_cast<int>(pen)     );

    if ((static_cast<int>(loptions)) & (static_cast<int>(straw)) == static_cast<int>(straw))
    {
        cout << "bring straw!" << endl;
    }

    system("PAUSE");
    return 0;
}

编辑:

即使我为枚举值添加唯一位集,它也不起作用。对于下面的代码,当我期望它显示 "bring cup" 时,它会忽略两个 if() 语句:

enum Stock
{
    milk = 1,
    choclate = 2,
    tv = 4,
    cable = 8,
    mouse = 16,
    fan = 32,
    headphone = 64,
    cup = 128,
    straw = 256,
    pen = 512,
    candy = 1024,
    glasses = 2048,
    book = 4096,
    plug = 8192

};


int main()
{
    Stock options = static_cast<Stock>(static_cast<int>(mouse) | static_cast<int>(headphone)
                                        | static_cast<int>(cup) | static_cast<int>(pen));

    if ((static_cast<int>(options)) & (static_cast<int>(straw)) == static_cast<int>(straw))
    {
        cout << "bring straw!" << endl;
    }

    if ((static_cast<int>(options)) & (static_cast<int>(cup)) == static_cast<int>(cup))
    {
        cout << "bring cup!" << endl;
    }

    system("PAUSE");
    return 0;
}

要将枚举用作位集(或标志),您需要确保每个枚举值的二进制表示恰好包含一个设置为 1 的位。换句话说,每个枚举值需要是 2 的幂。示例:

enum Stock  
{
    milk = 1,     // 0b0001
    choclate = 2, // 0b0010
    tv = 4,       // 0b0100
    cable = 8     // 0b1000
    // etc.
};

否则,按位逻辑运算符将无法区分某些值和其他值的某些组合。在原始代码中,chocolatetvcable的值分别为123。在二进制中,即 011011。 ORing chocolatetv 产生 11 (0b01 | 0b10 == 0b11),它与 ​​cable 的值相同。 chocolatetv 的组合与 cable 标志无法区分。

但是c++提供了std::bitset。 class 允许您以类似于位数组的方式轻松操作位集。然后,您可以只使用原始枚举并将每个枚举值用作位的 index