简体   繁体   中英

C# |= and &= operators


In an example I saw these operators (|= and &=) but it wasn't explained. I was looking on Google about it, but I found only results related to the "classic" = operator.
So I would like to know what are these operators doing. Can somebody explain it to me ?

They are simply shorthand assignments like += . The following are equivalent:

s |= t;
s = s | t;

And these are also equivalent.

s &= t;
s = s & t;

For more information on those operators, you can see the MSDN Docs on | and & Operator.

|= and &= are assignment operators related to the | (bitwise or) and & (bitwise and) operators.

They perform bitwise-OR |= operations and bitwise-AND &= operations with the result being stored in the lValue . They're the same as | and & , but store the result in the lValue analogous to the difference between + and += or - and -= .

well &= is the same like i+= , in other words

x&=2 is a short form of x=x & 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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