开发者

C# compound assignment operator ^=

What does this operator ^=开发者_如何学JAVA mean in 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.

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:

array[GetIndex()] ^= 10;

that would only call GetIndex once. But please don't do that, 'cos it's nasty :)

See also the relevant MSDN page.

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.


this:

x ^= y;

is equivalent to this:

x = x ^ y;

In words, set x to the value of x exclusive or'ed with 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. 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


This is the "exclusive or assignment" operator. Details are at 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜