Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compound assignment operator ^=

Tags:

c#

What does this operator ^= mean in c#?

like image 457
Joshua Slocum Avatar asked Apr 21 '11 13:04

Joshua Slocum


1 Answers

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.

like image 197
Jon Skeet Avatar answered Sep 28 '22 14:09

Jon Skeet