In some code i found this |=
operator used to return a uint
but i can find something about it on the internet and i wanna understand how it works and what are the return values in this case.
public uint Mask
{
get
{
uint num = 0;
if (_0)
num |= 1U;
if (_1)
num |= 2U;
if (_2)
num |= 4U;
return num;
}
}
a detailed answer will be much appreciated.
The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.
Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same: a = a | b; a |= b; The ' |= ' symbol is the bitwise OR assignment operator.
This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. For example: (a -= b) can be written as (a = a - b) If initially value stored in a is 8. Then (a -= 6) = 2.
For |= , think of the known examples, x +=5 . It means x = x + 5, therefore if we have x |= 5 , it means x = x bitwiseor with 5 .
You know how x += 1
means x = x + 1
, well x |= 1
means x = x | 1
. Of course |
means bitwise OR.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With