What does the |= operator mean in C++? - Stack Overflow
Is there something like "elif operator" in C programming?
How do bitwise operators work in C?
Newbie in C Programming here, I'm facing some confusion while using Increment operator.
What are Operators in C?
What are the Logic Operators in C?
What is the Difference Between the '=' and '==' Operators?
Videos
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. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.
The big advantage of the '|=' operator is when 'a' is itself a complex expression:
something[i].array[j]->bitfield |= 23;
vs:
something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;
Was that difference intentional or accidental?
...
Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:
something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;
Similar comments apply to all of the compound assignment operators:
+= -= *= /= %=
&= |= ^=
<<= >>=
Any compound operator expression:
a XX= b
is equivalent to:
a = (a) XX (b);
except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.
x |= y
same as
x = x | y
same as
x = x [BITWISE OR] y