Bitwise subtraction
-
26-01-2021 - |
Question
Given the enum:
[Flags]
enum foo
{
a = 1,
b = 2,
c = 4
}
then
foo example = a | b;
If I don't know if foo contains c
, previously I have been writing the following
if (example & foo.c == foo.c)
example = example ^ foo.c;
Is there a way to do this without checking for the existance of foo.c
in example
?
As when it comes to additions, I can just do an OR
, and if the enum value already exists in example
then it doesnt matter.
Solution
I think you want:
example &= ~foo.c;
In other words, perform a bitwise "AND" mask with every bit set except the one for c
.
EDIT: I should add an "except" to Unconstrained Melody at some point, so you could write:
example = example.Except(foo.c);
Let me know if this would be of interest to you, and I'll see what I can do over the weekend...
OTHER TIPS
AND it with the complement of foo.c:
example = example & ~foo.c
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow