简体   繁体   English

C#复合赋值运算符^=

[英]C# compound assignment operator ^=

这个运算符 ^= 在 C# 中是什么意思?

It means bitwise XOR the value of the LHS expression with the value of the RHS expression, and assign it back to the LHS expression.这意味着将 LHS 表达式的值与 RHS 表达式的值按位异或,并将其分配回 LHS 表达式。

So for example:例如:

int x = 10;
int y = 3;

x ^= y; // x = 10 ^ 3, i.e. 9

The LHS expression is only evaluated once, so if you have: LHS 表达式只计算一次,所以如果你有:

array[GetIndex()] ^= 10;

that would only call GetIndex once.那只会调用GetIndex一次。 But please don't do that, 'cos it's nasty :)但是请不要那样做,因为它很讨厌:)

See also the relevant MSDN page .另请参阅相关的 MSDN 页面

You may also find Eric Lippert's recent April Fool's Day blog post on compound assignment operators amusing - and part one of the series, which was rather more serious, may prove enlightening .您可能还会发现Eric Lippert 最近发表的关于复合赋值运算符的愚人节博文很有趣 - 而该系列的第一部分更为严肃,可能会很有启发性

this:这个:

x ^= y;

is equivalent to this:相当于:

x = x ^ y;

In words, set x to the value of x exclusive or'ed with y.换句话说,将 x 设置为 x 与 y 异或的值。

The exclusive-OR assignment operator.异或赋值运算符。

An expression of the form形式的表达

 x ^= y

is evaluated as被评估为

 x = x ^ y

except that x is only evaluated once.除了 x 只计算一次。 The ^ operator performs a bitwise exclusive-OR operation on integral operands and logical exclusive-OR on bool operands. ^ 运算符对整数操作数执行按位异或运算,对布尔操作数执行逻辑异或运算。

http://msdn.microsoft.com/en-us/library/0zbsw2z6.aspx http://msdn.microsoft.com/en-us/library/0zbsw2z6.aspx

This is the "exclusive or assignment" operator.这是“独占或赋值”运算符。 Details are athttp://msdn.microsoft.com/en-us/library/0zbsw2z6(v=VS.100).aspx详细信息位于http://msdn.microsoft.com/en-us/library/0zbsw2z6(v=VS.100).aspx

XOR.异或。 a ^= b is the same as a = a ^ b , where a and b are integer types of some sort. a ^= ba = a ^ b ,其中 a 和 b 是某种整数类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM