josefpospisil0
josefpospisil0

Reputation: 155

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 ?

Upvotes: 1

Views: 172

Answers (4)

itsme86
itsme86

Reputation: 19486

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 -=.

Upvotes: 1

Joey
Joey

Reputation: 354406

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

Upvotes: 1

Tigran
Tigran

Reputation: 62248

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

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

Upvotes: 0

mellamokb
mellamokb

Reputation: 56769

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.

Upvotes: 6

Related Questions