Introduction
The |= operator (pipe equal) is a compound assignment operator that performs a bitwise OR between the left operand and the right operand, then stores the result in the left operand. a |= b is equivalent to a = a | b. It is used in C, C++, Java, Python, JavaScript, and most C-family languages. The primary use case is setting specific bits in a bitmask — enabling flags, combining permissions, or building bit fields. In Python, |= also works on sets (union) and dictionaries (merge, Python 3.9+).
Bitwise OR Basics
1# Bitwise OR: each bit is 1 if EITHER input bit is 1
2a = 0b1010 # 10 in decimal
3b = 0b1100 # 12 in decimal
4
5result = a | b # 0b1110 = 14
6print(bin(result)) # 0b1110
7print(result) # 14
8
9# Truth table for each bit position:
10# 0 | 0 = 0
11# 0 | 1 = 1
12# 1 | 0 = 1
13# 1 | 1 = 1
Using |= for Bitmask Flags
1# Permission flags
2READ = 0b0001 # 1
3WRITE = 0b0010 # 2
4EXECUTE = 0b0100 # 4
5ADMIN = 0b1000 # 8
6
7# Start with no permissions
8permissions = 0
9
10# Grant permissions using |=
11permissions |= READ
12permissions |= WRITE
13print(bin(permissions)) # 0b0011 (read + write)
14
15# Grant multiple at once
16permissions |= EXECUTE | ADMIN
17print(bin(permissions)) # 0b1111 (all permissions)
18
19# Check if a flag is set using &
20has_read = bool(permissions & READ)
21print(has_read) # True
22
23# Remove a flag using &= ~FLAG
24permissions &= ~WRITE
25print(bin(permissions)) # 0b1101 (read, execute, admin — write removed)
|= sets bits to 1 without affecting other bits, making it ideal for enabling flags in a bitmask.
C/Java Bitmask Example
1#include <stdio.h>
2
3#define FLAG_VISIBLE (1 << 0) // 0x01
4#define FLAG_ENABLED (1 << 1) // 0x02
5#define FLAG_FOCUSED (1 << 2) // 0x04
6#define FLAG_SELECTED (1 << 3) // 0x08
7
8int main() {
9 unsigned int state = 0;
10
11 // Set flags
12 state |= FLAG_VISIBLE;
13 state |= FLAG_ENABLED;
14 printf("State: 0x%02X\n", state); // 0x03
15
16 // Set multiple flags
17 state |= FLAG_FOCUSED | FLAG_SELECTED;
18 printf("State: 0x%02X\n", state); // 0x0F
19
20 // Check flag
21 if (state & FLAG_VISIBLE) {
22 printf("Visible\n");
23 }
24
25 // Clear flag
26 state &= ~FLAG_FOCUSED;
27 printf("State: 0x%02X\n", state); // 0x0B
28
29 return 0;
30}
Python: |= with Sets
1# |= on sets performs union (adds elements)
2fruits = {"apple", "banana"}
3more = {"cherry", "banana", "date"}
4
5fruits |= more
6print(fruits) # {'apple', 'banana', 'cherry', 'date'}
7
8# Equivalent to:
9# fruits = fruits | more
10# fruits.update(more)
Python: |= with Dictionaries (3.9+)
1# |= on dicts performs merge (Python 3.9+)
2defaults = {"theme": "light", "lang": "en"}
3overrides = {"theme": "dark", "font_size": 14}
4
5defaults |= overrides
6print(defaults)
7# {'theme': 'dark', 'lang': 'en', 'font_size': 14}
8
9# Right-hand side wins on duplicate keys
JavaScript
1// Bitwise OR assignment
2let flags = 0;
3const READ = 1; // 0b001
4const WRITE = 2; // 0b010
5const EXEC = 4; // 0b100
6
7flags |= READ;
8flags |= WRITE;
9console.log(flags); // 3 (0b011)
10console.log(flags & READ); // 1 (truthy — READ is set)
11
12// Logical OR assignment (||=) is different — JavaScript only
13let a = 0;
14a ||= 5; // a = 5 (because 0 is falsy)
15// ||= assigns if left side is falsy — NOT the same as |=
1a = 0b1111
2
3# &= (AND assignment): clear bits
4a &= 0b1010 # a = 0b1010 (only bits set in BOTH)
5
6# ^= (XOR assignment): toggle bits
7a ^= 0b0011 # a = 0b1001 (flips bits where right is 1)
8
9# <<= (left shift assignment): multiply by power of 2
10a = 1
11a <<= 3 # a = 8 (1 shifted left 3 positions)
12
13# >>= (right shift assignment): divide by power of 2
14a = 16
15a >>= 2 # a = 4 (16 shifted right 2 positions)
Common Pitfalls
Confusing |= with ||=: |= is bitwise OR assignment. JavaScript's ||= is logical OR assignment (assigns only if the left side is falsy). Python does not have ||=.
Using |= on booleans: True |= False works in Python (booleans are integers) but is confusing. Use explicit boolean logic (a = a or b) for clarity.
Forgetting that |= only sets bits: |= can turn bits ON but never turns bits OFF. To clear bits, use &= ~FLAG. To toggle bits, use ^= FLAG.
Integer overflow in C: In C, |= on signed integers can cause undefined behavior if it results in a value outside the representable range. Use unsigned int for bitmask operations.
Assuming |= works on all Python types: |= works on integers (bitwise), sets (union), and dicts (merge, 3.9+). On other types, it raises TypeError unless the class defines __ior__.
Summary
a |= b is shorthand for a = a | b — bitwise OR and assign
Primary use case: setting flags in a bitmask without affecting other bits
Python: also works on sets (union) and dicts (merge, Python 3.9+)
Use &= to clear bits, ^= to toggle bits, |= to set bits
Do not confuse with JavaScript's ||= (logical OR assignment)
Related compound operators: &=, ^=, <<=, >>=