Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsetting an enum flag

Tags:

c#

Consider

   [Flags]    public enum State    {       IsCool = 0x1,       SomethingElse = 0x2    } 

I have a State someState and if some expression evaluates to true, I want to unset the IsCool flag of someState regardless of it being already set or unset. This means that I can't really use someState ^= State.IsCool but what can I use instead?

like image 922
kasperhj Avatar asked Jun 13 '12 08:06

kasperhj


1 Answers

You need to approach this the reverse way than when setting a flag: bitwise AND the current state with the complement of the flag you want to remove.

someState &= ~State.IsCool; 
like image 128
Jon Avatar answered Sep 20 '22 04:09

Jon