简体   繁体   English

C ++ / WinAPI-从DWORD中删除标志(按位运算符?)

[英]C++/WinAPI - Removing a flag from a DWORD (bitwise operator?)

I now know how to check if there is a certain flag in a DWORD (Specifically a Windows style) by using the bitwise & AND operator. 我现在知道如何使用按位& AND运算符检查DWORD(特别是Windows样式)中是否存在某些标志。 How would I do this: 我该怎么做:

if (dwMyFlags & dwSomeFlag) {
    // dwMyFlags contains dwSomeFlag
    // ->> How do I remove dwSomeFlag from dwMyFlags?
}

I know subtracting it wouldn't work, is there some operator that can remove flags from a DWORD? 我知道减去它是行不通的,是否有一些运算符可以从DWORD中删除标志?

If you know the flag is set you can use exclusive or to unset: 如果您知道该标志已设置,则可以使用独占或取消设置:

dwMyFlags ^= dwSomeFlag;

If you are unsure of the state of the flag, you need to use bitwise &, and bitwise not ~ 如果不确定标志的状态,则需要使用按位&,而不是按位〜

dwMyFlags &= ~dwSomeFlag;

Do a bitwise & with the inverse of the flag that you want: 用所需的标志的位进行按位&运算:

dwMyFlags = dwMyFlags & ~dwSomeFlag;

You can abbreviate this using &= : 您可以使用&=对此进行缩写:

dwMyFlags &= ~dwSomeFlag;

AND with the bits that are not part of that flag: 与那些不属于该标志的位:

dwMyFlags &= ~dwSomeFlag;

This is scalable to removing multiple flags as well: 这也可以扩展为删除多个标志:

dwMyFlags &= ~(dwSomeFlag | dwSomeOtherFlag);

Also, Hungarian notation has outlived its use. 同样,匈牙利符号的使用已经过期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM