archies50
archies50

Reputation: 103

Meaning of "| =" operator?

I have encountered code like

if (flagsDef) 
flagsTainted |= flagsUsed;

please assist in knowing the meaning of the operator used.

Upvotes: 3

Views: 245

Answers (4)

Jordan Kaye
Jordan Kaye

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

Karoly Horvath
Karoly Horvath

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

pb2q
pb2q

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

devshorts
devshorts

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

Related Questions