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...
我用'&'和'|'用于按位'和'和'或'用枚举,但这是我第一次看到那个符号......
4 个解决方案
#1
22
^ is the bitwise XOR operator in C#.
^是C#中的按位XOR运算符。
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.
编辑:如果a为真且b为假或a为假且b为真,则a ^ b返回true,但不是两者。
#2
15
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.
这将是'xor'运营商。在您的示例代码中,它将打开或关闭Flags.Hidden,具体取决于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.
这样做的好处是,它允许您更改Flags.Hidden的设置,而不会影响已设置的任何其他标志。
#3
2
It's the exclusive OR (XOR) operator, this link has example usage
它是独占的OR(XOR)运算符,此链接具有示例用法
#4
2
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.
对于整数类型,^计算其操作数的按位异或。对于bool操作数,^计算其操作数的逻辑异或;也就是说,当且仅当奇数个操作数为真时,结果才为真。
#1
22
^ is the bitwise XOR operator in C#.
^是C#中的按位XOR运算符。
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.
编辑:如果a为真且b为假或a为假且b为真,则a ^ b返回true,但不是两者。
#2
15
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.
这将是'xor'运营商。在您的示例代码中,它将打开或关闭Flags.Hidden,具体取决于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.
这样做的好处是,它允许您更改Flags.Hidden的设置,而不会影响已设置的任何其他标志。
#3
2
It's the exclusive OR (XOR) operator, this link has example usage
它是独占的OR(XOR)运算符,此链接具有示例用法
#4
2
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.
对于整数类型,^计算其操作数的按位异或。对于bool操作数,^计算其操作数的逻辑异或;也就是说,当且仅当奇数个操作数为真时,结果才为真。