Reputation: 103
I have encountered code like
if (flagsDef)
flagsTainted |= flagsUsed;
please assist in knowing the meaning of the operator used.
Upvotes: 3
Views: 245
Reputation: 2887
It can be read in English as "or equals".
It's similar to += except instead of adding the left value to the right, it instead performs a bitwise or of the two values and then assigns the result into the left variable as you would expect.
For more information on bitwise operations, see this link: Wikipedia
Upvotes: 0
Reputation: 96258
a op= b
is a = a op b
, and |
is the bitwise or
operator (bitwise meaning it's applied for each binary digit).
Here is the truth table for or
:
0 1
___
0| 0 1
1| 1 1
Upvotes: 3
Reputation: 59607
The statement:
flagsTainted |= flagsUsed
is shorthand for:
flagsTainted = flagsTainted | flagsUsed
which uses the binary/bitwise OR operator |
.
The code is manipulating a flag variable, which is keeping state information by setting bits in the variable flagsTainted
.
For more information about bitwise manipulation, the wikipedia article is pretty good.
Upvotes: 12
Reputation: 8872
|
is a bitwise OR. This means that it compares the bits using an or operator.
For example:
101
001
If you |
the two, you get 101. The | = assigns the result back to the left hand side of the operation.
Upvotes: 1