C ++运算符& enum评估为false

时间:2017-02-08 21:45:33

标签: c++

以下代码输出reportAllDataRanges = 0。

BOOLEAN updateDataRanges = IsFirstUpdate || (m_uiDataRangeMode & IAdapterCommon::UpdatePinDataRanges);
BOOLEAN updateConstrainedDataRanges = m_uiDataRangeMode & IAdapterCommon::UpdatePinConstrainedDataRanges;
BOOLEAN reportAllDataRanges = m_uiDataRangeMode & IAdapterCommon::ReportAllDataRanges;

DPF_ENTER(("[CAdapterCommon::UpdatePinDescriptor(%p, %lu)] - m_uiDataRangeMode = %lu, updateDataRanges = %lu, updateConstrainedDataRanges = %lu, reportAllDataRanges = %lu, a & b = %lu, b = %lu", PinDescriptor, IsFirstUpdate, m_uiDataRangeMode, updateDataRanges, updateConstrainedDataRanges, reportAllDataRanges, m_uiDataRangeMode & IAdapterCommon::ReportAllDataRanges, IAdapterCommon::ReportAllDataRanges));
[CAdapterCommon::UpdatePinDescriptor(FFFFF8064CCE42F0, 1)] - m_uiDataRangeMode = 3070, updateDataRanges = 1, updateConstrainedDataRanges = 64, reportAllDataRanges = 0, a & b = 512, b = 512
typedef enum : UINT32
{
    None = 0,
    UseDataRanges = 1 << 0,
    UseDiscreteDataRanges = 1 << 1,
    RaiseUpdateEvent = 1 << 2,
    RaiseUpdatePinDescriptor = 1 << 3,
    DoNotUpdateOriginalDescriptor = 1 << 4,
    UpdatePinDataRanges = 1 << 5,
    UpdatePinConstrainedDataRanges = 1 << 6,
    UseDataRangeIntersection = 1 << 7,
    UseProposedDataFormat = 1 << 8,
    ReportAllDataRanges = 1 << 9,
    DataRangeIntersectionAcceptAllRanges = 1 << 10,
    SupportedDataRangesFirst = 1 << 11,
} DataRangeModeEnum;

不应该

BOOLEAN value = 3070 & 512;

评估为TRUE? (大于零)?

2 个答案:

答案 0 :(得分:3)

我认为你在tile标题中使用BOOLEAN类型,它是BYTE类型的typedef,它是unsinged char类型的typedef,它可以保存0到255之间的值而不是{ {1}}表达式计算。有一个溢出导致值为0,相当于<Windows.h>

答案 1 :(得分:2)

我想发生的事情是BOOLEAN是1字节类型的typedef。例如。在Microsoft的Windows标头中,它是unsigned charlink)的typedef。

3070 & 512的值为512。将512分配给unsigned char会得到0的结果。

要避免此问题,您可以执行以下操作之一:

  • 使用bool代替BOOLEAN
  • 使用!!(x & y)代替x & y
  • 使用(x & y) == y