gpt4 book ai didi

c - bool 代数 NOT 和 DWORD - 建议修复

转载 作者:行者123 更新时间:2023-11-30 19:06:31 28 4
gpt4 key购买 nike

我发现一些遗留代码在设置文件属性时出错。它看起来像这样 -

flag1 = 0x0001;
flag2 = 0x0002;

DWORD flags = flag1 | flag2;

//great - flags is 3

DWORD prevValue = 0x0010; //say

//add our new flags to prevValue
DWORD newVal = prevValue | flags;

//newVal is 19

//but now I want to remove those flags from newVal

DWORD backToPrev = newVal & !flags;

//but according to my compiler (vs2012)
//!flags is 0, it can't do the logical NOT on the DWORD

ASSERT (backToPrev); // it's 0, should be 16

有什么修复建议吗?

最佳答案

! 运算符是逻辑 NOT。该运算符的结果是 0 或 1。您想要的是按位 NOT 运算符 ~:

DWORD backToPrev = newVal & ~flags;

关于c - bool 代数 NOT 和 DWORD - 建议修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47909646/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com