I was reading some 3rd party code and I found this:
x.Flags = x.Flags ^ Flags.Hidden;
What does it do?
I've used '&' and '|' for bitwise 'and' and 'or' with enums, but it's the first time I see the that symbol...
I was reading some 3rd party code and I found this:
x.Flags = x.Flags ^ Flags.Hidden;
What does it do?
I've used '&' and '|' for bitwise 'and' and 'or' with enums, but it's the first time I see the that symbol...
^ is the bitwise XOR operator in C#.
EDIT: a ^ b returns true if a is true and b is false or if a is false and b is true, but not both.
Taken from here:
For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.
It's the exclusive OR (XOR) operator, this link has example usage
That would be the 'xor' operator. In your example code, it would toggle the Flags.Hidden either on or off, depending on the current value of x.Flags.
The benefit of doing it this way is that it allows you to change the setting for Flags.Hidden without affecting any other flags that have been set.