naspinski
naspinski

Reputation: 34689

In C#, what does '|=' do?

I can't seem to Google it - doesn't appear to like the syntax in the search string. Thank you for the help.

Upvotes: 10

Views: 10807

Answers (7)

Ry-
Ry-

Reputation: 224903

The expression a |= b is equivalent to the assignment a = a | b, where | is the bitwise OR operator.*

* Not entirely, but close enough for most purposes.

Upvotes: 5

Hounshell
Hounshell

Reputation: 5459

It's like +=, but with the binary OR

int x = 5;
x |= 6;  // x is now 7: 5 | 6

You can also do others like &=, /=, *=, etc. Pretty much any binary (two argument) operator

Upvotes: 3

Marc
Marc

Reputation: 16512

you gonna find the answer here on msdn

x |= y

is the same as

x = x | y

Upvotes: 1

Dave S
Dave S

Reputation: 775

|= is a bitwise OR assignment operator. Check out the msdn documentation here http://msdn.microsoft.com/en-us/library/h5f1zzaw(v=vs.100).aspx

Upvotes: 3

Rune FS
Rune FS

Reputation: 21742

a |= b is semantically the same as a = a | b

Upvotes: 1

JaredPar
JaredPar

Reputation: 754715

This is a bit wise assignment. It's roughly shorthand for the following

x |= y;
x = x | y;

Note: It's not truly the above because the C# spec guarantees the side effects of x only occur once. So if x is a complex expression there is a bit of fun code generated by the compiler to ensure that the side effects only happen once.

Method().x |= y;
Method().x = Method().x | y;  // Not equivalent

var temp = Method();
temp.x = temp.x | y;  // Pretty close

Upvotes: 18

Jonathan Bondhus
Jonathan Bondhus

Reputation: 301

In C and other languages following C syntax conventions, such as C++, Perl, Java and C#, (a | b) denotes a bitwise or; whilst a double vertical bar (a || b) denotes a (short-circuited) logical or.

Upvotes: 0

Related Questions