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.
source share