What does |= mean? (pipe equal operator)
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The |= operator is the bitwise OR assignment operator. It performs a bitwise OR between the left operand and the right operand, then assigns the result back to the left operand. The expression x |= y is shorthand for x = x | y. It is available in C, C++, Java, Python, JavaScript, Go, Rust, and most other languages that support bitwise operations.
How Bitwise OR Works
Before understanding |=, you need to understand the bitwise OR operator (|). It compares each bit of two integers and produces a 1 if either bit is 1:
The rule for each bit position: if either input bit is 1, the output bit is 1. Only when both input bits are 0 does the output produce 0.
The |= Operator in Action
|= combines the OR operation with assignment. Instead of writing x = x | y, you write x |= y:
This pattern follows the same convention as other compound assignment operators like +=, -=, *=, and &=.
Primary Use Case: Setting Bit Flags
The most common real-world use of |= is setting specific bits in a flag variable. This pattern is used extensively in systems programming, file permissions, feature toggles, and hardware registers.
Unix File Permissions Example
Each call to |= turns on the specified bit without affecting other bits. This is why OR is used for "setting" flags: ORing a 0-bit with 1 sets it to 1, and ORing a 1-bit with 1 leaves it at 1.
Feature Flags in C
Java Enum Flags with Bit Manipulation
The Companion Operators: &= and ^=
Understanding |= is easier in context with its sibling bitwise assignment operators:
| Operator | Name | Operation | Use Case |
|= | OR assignment | Sets bits to 1 | Turning on flags |
&= | AND assignment | Keeps bits that are 1 in both | Turning off flags (with inverted mask) |
^= | XOR assignment | Flips bits | Toggling flags |
<<= | Left shift assignment | Shifts bits left | Multiplying by powers of 2 |
>>= | Right shift assignment | Shifts bits right | Dividing by powers of 2 |
Here is how they work together to manage flags:
|= with Python Sets
In Python, |= has a second meaning when used with sets. It performs a set union (in-place):
Python 3.9+ also supports |= for dictionary merging:
|= in JavaScript
JavaScript supports |= for bitwise operations, but with a caveat: JavaScript numbers are 64-bit floats, and bitwise operations convert them to 32-bit signed integers first:
JavaScript also has the logical OR assignment (||=) since ES2021, which is different from |=:
Common Pitfalls
Confusing |= (bitwise OR) with ||= (logical OR assignment). In JavaScript, |= performs bitwise OR on integers, while ||= performs logical OR and works with any type. In Python, |= works on integers (bitwise) and sets/dicts (union), but there is no ||= operator.
Using |= on signed integers without understanding sign extension. In Java and C, right-shifting a negative number with >> sign-extends (fills with 1s). If you |= a sign-extended value, you may set more bits than intended. Use unsigned types or >>> (unsigned right shift in Java) when working with bit flags.
Forgetting that |= only sets bits, never clears them. ORing with 0 leaves a bit unchanged. ORing with 1 sets it to 1. You cannot turn off a bit with |=. Use &= ~flag to clear a specific bit.
Operator precedence surprises. In expressions like x |= a & b, the & is evaluated first because it has higher precedence than |=. This is usually the desired behavior, but add parentheses when combining multiple bitwise operators to make intent explicit.
Assuming |= works the same on all Python types. For integers, |= does bitwise OR. For sets, it does union. For dicts (3.9+), it does merge. The behavior depends entirely on the type's __ior__ method implementation.
Summary
The |= operator is the bitwise OR assignment operator, equivalent to x = x | y. Its primary use is setting specific bits in flag variables without affecting other bits. This pattern appears throughout systems programming, permission systems, feature toggles, and hardware interfaces. In Python, |= also serves as the in-place set union and dictionary merge operator. Pair it with &= for clearing bits and ^= for toggling bits to have a complete toolkit for bit manipulation.

