我有一个像:
public enum Blah
{
RED = 2,
BLUE = 4,
GREEN = 8,
YELLOW = 16
}
Blah colors = Blah.RED | Blah.BLUE | Blah.YELLOW;
Run Code Online (Sandbox Code Playgroud)
如何从可变颜色中删除蓝色?
SLa*_*aks 298
你需要&
使用~
'补充'的'蓝色'.
补码运算符实质上反转或"翻转"给定数据类型的所有位.因此,如果您使用带有某个值的AND
operator(&
)(让我们调用该值'X')和一个或多个设置位的补码(让我们调用那些位Q
及其补码~Q
),该语句将X & ~Q
清除在中设置的任何位.Q
来自X
并返回结果.
因此,要删除或清除这些BLUE
位,请使用以下语句:
colorsWithoutBlue = colors & ~Blah.BLUE
colors &= ~Blah.BLUE // This one removes the bit from 'colors' itself
Run Code Online (Sandbox Code Playgroud)
您还可以指定要清除的多个位,如下所示:
colorsWithoutBlueOrRed = colors & ~(Blah.BLUE | Blah.RED)
colors &= ~(Blah.BLUE | Blah.RED) // This one removes both bits from 'colors' itself
Run Code Online (Sandbox Code Playgroud)
或者......
colorsWithoutBlueOrRed = colors & ~Blah.BLUE & ~Blah.RED
colors &= ~Blah.BLUE & ~Blah.RED // This one removes both bits from 'colors' itself
Run Code Online (Sandbox Code Playgroud)
总结一下:
X | Q
设置位 Q
X & ~Q
清除位 Q
~X
翻转/反转所有位 X
par*_*par 43
其他答案是正确的,但要从上面特别删除蓝色,你会写:
colors &= ~Blah.BLUE;
Run Code Online (Sandbox Code Playgroud)
And not
它...............................
Blah.RED | Blah.YELLOW ==
(Blah.RED | Blah.BLUE | Blah.YELLOW) & ~Blah.BLUE;
Run Code Online (Sandbox Code Playgroud)
认为这可能对像我这样偶然发现的其他人有用.
请注意如何处理您可能设置为具有值== 0的任何枚举值(有时,对于枚举具有"未知"或"空闲"状态会有所帮助).当依赖这些位操作操作时,它会导致问题.
此外,当您的枚举值是2个值的其他幂的组合时,例如
public enum Colour
{
None = 0, // default value
RED = 2,
BLUE = 4,
GREEN = 8,
YELLOW = 16,
Orange = 18 // Combined value of RED and YELLOW
}
Run Code Online (Sandbox Code Playgroud)
在这些情况下,这样的扩展方法可能派上用场:
public static Colour UnSet(this Colour states, Colour state)
{
if ((int)states == 0)
return states;
if (states == state)
return Colour.None;
return states & ~state;
}
Run Code Online (Sandbox Code Playgroud)
以及处理组合值的等效IsSet方法(虽然有点像hacky方式)
public static bool IsSet(this Colour states, Colour state)
{
// By default if not OR'd
if (states == state)
return true;
// Combined: One or more bits need to be set
if( state == Colour.Orange )
return 0 != (int)(states & state);
// Non-combined: all bits need to be set
return (states & state) == state;
}
Run Code Online (Sandbox Code Playgroud)
为了简化标志枚举并通过避免倍数来使其更好地读取,我们可以使用位移位。(摘自一篇好文章《结束关于枚举标志的大辩论》)
[FLAG]
Enum Blah
{
RED = 1,
BLUE = 1 << 1,
GREEN = 1 << 2,
YELLOW = 1 << 3
}
Run Code Online (Sandbox Code Playgroud)
并清除所有位
private static void ClearAllBits()
{
colors = colors & ~colors;
}
Run Code Online (Sandbox Code Playgroud)