I want to know the value of result
in the following two cases:
1.
var result = false;
result |= false;
2.
var result = false;
result |= true;
I want to know the value of result
in the following two cases:
1.
var result = false;
result |= false;
2.
var result = false;
result |= true;
it's the BitWise OR Assignment. See http://msdn.microsoft.com/en-us/library/ie/81bads72%28v=vs.94%29.aspx
so result |= expression
is the same as result = result | expression
The |
operator is the Bitwise OR operator. Rather than doing a standard binary OR (truth table below) the |
operator will perform a binary OR for each bit of both operands.
Binary OR
x|y|q
0|0|0
1|0|1
0|1|1
1|1|1
Example
3 | 5
// in binary
0011 | 0101
// as a truth table
x|y|q
0|0|0
0|1|1
1|0|1
1|1|1
0011 | 0101 = 0111
// in decimal
3 | 5 = 7
Using the Bitwise OR operator in conjunction with an equals sign turns it into an assignment statement, in a similar way to +=
or -=
or any of the other logic/arithmetic operators. It is just a shorthand for result = result | operand
.
It's important to note that if you use the operator with boolean values (true or false), they are coerced into numeric values (1 and 0, respectively) before being compared in the above way.
It is an Assignment Operator of a BitWise Operator "|" which returns a one in each bit position for which the corresponding bits of either or both operands are ones.