【问题标题】:Why bitwise OR and AND logic is not working as expected?为什么按位 OR 和 AND 逻辑没有按预期工作?
【发布时间】:2019-10-08 19:38:46
【问题描述】:

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

例如,如果我做result = mouse | headphone,那么我可以检查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;
}

【问题讨论】:

  • 您假设每个enum 的值都是唯一的位。事实并非如此。 enums 的值总是比前一个枚举值高 1,除非另有规定。
  • @LambaDawet 是的,但唯一值并不意味着唯一不同的位。唯一位意味着需要使用值 1、2、4、8 等。例如cable == tv | chocolatemilk 为零,甚至没有任何位为 1。也许您对二进制 AND 和 OR 运算符的工作方式感到困惑。
  • 要进行按位检查,不应该将不同的参数组合在一起(并用 AND 进行检查)都是 2 的幂(即 1,2,4,8,... ),而不是线性范围 0,1,2,3,4,5? (与@FrançoisAndrieux 的评论一致)
  • 您的编辑中括号放错了。写if ((options &amp; cup) == cup)就够了,static_casts没用。但重要的是(options &amp; cup) == cup 而不是options &amp; (cup == cup)

标签: c++ c++11 enums bitwise-operators


【解决方案1】:

要将枚举用作位集(或标志),您需要确保每个枚举值的二进制表示恰好包含一个设置为 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。此类允许您以类似于位数组的方式轻松操作位集。然后,您可以只使用原始枚举并将每个枚举值用作位的 index

【讨论】:

  • 我像你一样添加了值,如果我像 straw 那样做if( for the cup ),它会忽略这两个语句并且不显示任何内容。你能查一下吗?
  • @LambaDawet 我在尝试您的代码时收到“'&':检查运算符优先级以查找可能的错误;使用括号来阐明优先级”警告。 == 发生在 &amp; 之前。您可以使用等式左侧的括号来解决此问题。但更简单的解决方案是完全消除相等性。如果x 具有与y 相同的任何位,则仅x &amp; y 将产生true。无需与y比较。
  • 知道了!再次感谢!
  • "ORing 巧克力和电视产生 11 (0b01 | 0b10 == 0b11),这与电缆的值相同。" - 那是假的!
  • @Ajay 需要详细说明吗?
猜你喜欢
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 2014-08-04
  • 2021-01-03
  • 2018-11-08
相关资源
最近更新 更多