views:

1006

answers:

4

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...

+17  A: 

^ 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.

Romain Verdier
My memory of bitwise operations are a bit rusty... what would an XOR be doing in this situation?
Adam Lassek
Return true if and only if exactly one of the operands is true.
Santiago Palladino
It would toggle the Flags.Hidden bit in x.Flags. (If it's 1, it'll be 0; if it's 0, it'll be 1).
yjerem
+2  A: 

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.

Ryan Duffield
+2  A: 

It's the exclusive OR (XOR) operator, this link has example usage

http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx

PhilGriffin
Phil...are they not working you hard enough? :)
Kev
+11  A: 

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.

Jeromy Irvine