TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
|= operator is the bitwise inclusive OR assignment operator in C++. It performs a bitwise OR operation between the left and right operands, subsequently assigning the computed result directly to the left operand.
A |= B is equivalent to A = A | B, with the crucial technical distinction that the left operand A is evaluated exactly once. This single evaluation is critical when the left operand contains side effects (e.g., arr[i++] |= 5;). The built-in operator returns an lvalue reference to the modified left operand.
Bitwise Mechanics
The operator evaluates the operands at the binary level, comparing them bit by bit. For each corresponding bit position, the resulting bit is set to1 if at least one of the operand bits is 1. It is set to 0 only if both operand bits are 0. Because the left operand of |= must be a modifiable lvalue, the bit-level logic is demonstrated below using the standard bitwise OR operator (|):
0 | 0yields00 | 1yields11 | 0yields11 | 1yields1
Type Constraints, Conversions, and Deprecations
For the built-in operator, the left operand must be a modifiable lvalue of an integral type (e.g.,int, char, short, long, unsigned variants). The right operand must be of an integral or unscoped enumeration type.
If the operands possess different types, C++ applies standard integral promotions and usual arithmetic conversions before executing the bitwise operation. The final result is then implicitly converted back to the integral type of the left operand before assignment.
The built-in |= operator is ill-formed if the left operand is an enumeration type. This is because the bitwise OR operation promotes the operands to an integer, and C++ does not allow implicit conversions from an integer back to an enumeration type. To use |= with an enumeration as the left operand, the operator must be explicitly overloaded for that specific enumeration.
C++20 Deprecation: Starting in C++20, applying compound assignment operators, including |=, to a volatile-qualified left operand is deprecated.
Execution Example
Operator Overloading
The|= operator can be overloaded for user-defined types (classes, structs, or enumerations). When overloading for a class or struct, it is standard practice to implement it as a member function that modifies the object’s state and returns an lvalue reference to the modified object (*this), maintaining consistency with the value category and return type of the built-in compound assignment semantics.
Master C++ with Deep Grasping Methodology!Learn More





