What does operator | = in C ++?

What does operator | = in C ++?

+4
source share
4 answers

Assuming you use the built-in operators for integers or the safe overloaded operators for custom classes, they are the same:

a = a | b; a |= b; 

The character ' |= ' is a bitwise OR assignment operator. It computes the value of OR'ing RHS ('b') with LHS ('a') and assigns the result to "a", but only evaluates to "a" once.

A big advantage to the operator '| = 'is that' a 'itself is a complex expression:

 something[i].array[j]->bitfield |= 23; 

vs

 something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23; 

Was this distinction intentional or accidental?

...

Answer: intentionally - to show the advantage of the abbreviated expression ... the first of complex expressions is actually equivalent:

 something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23; 

Similar comments apply to all compound assignment operators:

 += -= *= /= %= &= |= ^= <<= >>= 

Any expression of a compound statement:

 a XX= b 

is equivalent to:

 a = (a) XX (b); 

except that a is evaluated only once. Pay attention to the parentheses here - this shows how grouping works.

+23
source
 x |= y 

same as

 x = x | y 

same as

 x = x [BITWISE OR] y 
+8
source

This is a bitwise or composite assignment .

Just like you can write x += y to mean x = x + y

you can write x |= y as meaning x = x | y x = x | y , which OR concatenates all bits of x and y , and then puts the result in x .

Beware that this can be overloaded , but for basic types you should be fine :-)

+3
source

You can use SymbolHound: a search engine for programmers to search for sites like SO for such characters. Here are the results for | = on SymbolHound. -Tom (SH co-founder)

+1
source

All Articles