What does '^' do in c# (Enums)?

前端 未结 4 825
不思量自难忘°
不思量自难忘° 2021-01-12 02:40

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 \'|

相关标签:
4条回答
  • 2021-01-12 02:59

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

    0 讨论(0)
  • 2021-01-12 03:07

    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.

    0 讨论(0)
  • 2021-01-12 03:07

    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 an odd number of its operands is true.

    0 讨论(0)
  • 2021-01-12 03:16

    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

    0 讨论(0)
提交回复
热议问题