Programming
Coding Symbols
Pipe Equal Operator
Code Explanation
Programming Language Syntax

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:

 
1  1 0 1 0   (decimal 10)
2| 0 1 1 0   (decimal 6)
3---------
4  1 1 1 0   (decimal 14)

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.

python
1# Python demonstration
2a = 10       # binary: 1010
3b = 6        # binary: 0110
4result = a | b  # binary: 1110 = decimal 14
5print(result)   # 14
6print(bin(result))  # 0b1110

The |= Operator in Action

|= combines the OR operation with assignment. Instead of writing x = x | y, you write x |= y:

python
1# These two are equivalent
2x = 10
3x = x | 6   # x is now 14
4
5x = 10
6x |= 6      # x is now 14

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

python
1# Unix permission flags
2READ    = 0b100  # 4
3WRITE   = 0b010  # 2
4EXECUTE = 0b001  # 1
5
6# Start with no permissions
7permissions = 0b000  # 0
8
9# Grant read permission
10permissions |= READ
11print(bin(permissions))  # 0b100 (4)
12
13# Grant write permission
14permissions |= WRITE
15print(bin(permissions))  # 0b110 (6)
16
17# Grant execute permission
18permissions |= EXECUTE
19print(bin(permissions))  # 0b111 (7) = rwx

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

c
1#include <stdio.h>
2
3#define FEATURE_LOGGING    (1 << 0)  // 0001
4#define FEATURE_CACHE      (1 << 1)  // 0010
5#define FEATURE_METRICS    (1 << 2)  // 0100
6#define FEATURE_DARK_MODE  (1 << 3)  // 1000
7
8int main() {
9    unsigned int features = 0;
10
11    // Enable logging and caching
12    features |= FEATURE_LOGGING;
13    features |= FEATURE_CACHE;
14
15    // Enable multiple features at once
16    features |= (FEATURE_METRICS | FEATURE_DARK_MODE);
17
18    // Check if a feature is enabled
19    if (features & FEATURE_CACHE) {
20        printf("Cache is enabled\n");
21    }
22
23    // features is now 0b1111 (15) - all four features enabled
24    printf("Features: %u\n", features);
25    return 0;
26}

Java Enum Flags with Bit Manipulation

java
1public class Permissions {
2    public static final int NONE    = 0;
3    public static final int READ    = 1 << 0;  // 1
4    public static final int WRITE   = 1 << 1;  // 2
5    public static final int DELETE  = 1 << 2;  // 4
6    public static final int ADMIN   = 1 << 3;  // 8
7
8    public static void main(String[] args) {
9        int userPerms = NONE;
10
11        // Grant permissions
12        userPerms |= READ;
13        userPerms |= WRITE;
14
15        // Check permission
16        boolean canRead = (userPerms & READ) != 0;  // true
17        boolean canDelete = (userPerms & DELETE) != 0;  // false
18
19        System.out.println("Can read: " + canRead);
20        System.out.println("Can delete: " + canDelete);
21    }
22}

The Companion Operators: &= and ^=

Understanding |= is easier in context with its sibling bitwise assignment operators:

OperatorNameOperationUse Case
|=OR assignmentSets bits to 1Turning on flags
&=AND assignmentKeeps bits that are 1 in bothTurning off flags (with inverted mask)
^=XOR assignmentFlips bitsToggling flags
<<=Left shift assignmentShifts bits leftMultiplying by powers of 2
>>=Right shift assignmentShifts bits rightDividing by powers of 2

Here is how they work together to manage flags:

python
1READ  = 0b100
2WRITE = 0b010
3EXEC  = 0b001
4
5perms = 0b000
6
7# SET a flag: use |=
8perms |= READ          # perms = 0b100
9
10# CHECK a flag: use &
11has_read = bool(perms & READ)  # True
12
13# CLEAR a flag: use &= with inverted mask
14perms &= ~WRITE       # perms = 0b100 (WRITE was already off)
15perms |= WRITE        # perms = 0b110 (turn WRITE on first)
16perms &= ~WRITE       # perms = 0b100 (now WRITE is cleared)
17
18# TOGGLE a flag: use ^=
19perms ^= EXEC         # perms = 0b101 (EXEC was off, now on)
20perms ^= EXEC         # perms = 0b100 (EXEC was on, now off)

|= with Python Sets

In Python, |= has a second meaning when used with sets. It performs a set union (in-place):

python
1# Set union with |=
2colors = {"red", "blue"}
3more_colors = {"blue", "green", "yellow"}
4
5colors |= more_colors
6print(colors)  # {'red', 'blue', 'green', 'yellow'}
7
8# This is equivalent to
9colors = colors | more_colors
10# or
11colors.update(more_colors)

Python 3.9+ also supports |= for dictionary merging:

python
1# Dictionary merge with |= (Python 3.9+)
2defaults = {"theme": "light", "lang": "en"}
3overrides = {"theme": "dark", "font_size": 14}
4
5defaults |= overrides
6print(defaults)  # {'theme': 'dark', 'lang': 'en', 'font_size': 14}

|= 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
1let flags = 0;
2
3const VERBOSE = 1 << 0;  // 1
4const DEBUG   = 1 << 1;  // 2
5const TRACE   = 1 << 2;  // 4
6
7flags |= VERBOSE;
8flags |= DEBUG;
9
10console.log(flags);  // 3
11console.log(flags & VERBOSE);  // 1 (truthy)
12console.log(flags & TRACE);   // 0 (falsy)

JavaScript also has the logical OR assignment (||=) since ES2021, which is different from |=:

javascript
1// ||= assigns only if the left side is falsy
2let name = "";
3name ||= "default";   // name is now "default"
4
5// |= always performs bitwise OR
6let x = 0;
7x |= 0;  // x is still 0 (bitwise OR of 0 and 0)

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.


Course illustration
Course illustration

All Rights Reserved.