Operators
Lesson 50: Using |= Compound Bitwise OR Operator
Learn how |= sets selected bits while keeping existing bits, useful for flags, masks, and register-style updates.
Progress indicator
Lesson 50 of 57
Learning Objectives
- Understand what the compound bitwise OR operator (|=) does.
- Use |= to set selected bits while preserving existing bits.
- Compare |= with writing | and = separately.
- Apply |= with bit masks for embedded flag control.
- Avoid common confusion between bitwise and logical OR.
- Predict flag changes by reading binary transitions.
Concept Explanation
What is the Compound Bitwise OR Operator (|=)
The |= operator applies bitwise OR and stores the result back into the same variable.
It is a short way to write value = value | mask.
Compound Bitwise OR Syntax
flags |= FLAG_ENABLE;
registerValue |= 0b00000100;How |= Works
- Read current bits from the variable.
- Read mask bits from the right side.
- Apply bitwise OR at each bit position.
- Store resulting bits back into the same variable.
Rule reminder: if either bit is 1, the result bit becomes 1.
| Bit A | Bit B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Updating Variables with |=
flags |= 0b00000001sets bit 0.flags |= 0b00000100sets bit 2 without clearing others.config |= MODE_MASKenables selected mode bits.
|= vs | and = (Comparison)
x = x | maskandx |= maskare equivalent.|=is shorter and clearer in repeated flag updates.
x = x | mask; // long form
x |= mask; // compound formUsing |= with Bit Masks
Use mask bits with 1 where you want to set a bit. Bits with 0 in the mask stay unchanged.
| Step | Original flags | Mask | Result after |= |
|---|---|---|---|
| Enable LED | 00000000 | 00000001 | 00000001 |
| Enable logging | 00000001 | 00000100 | 00000101 |
Data Type Notes
- Use integer types for bitwise operators (
byte,int). byteis easiest for beginner binary flag examples.- Use mask widths that match your variable width to avoid confusion.
When to Use |=
- Enable one or more feature flags.
- Build composite status bits over time.
- Set specific configuration bits safely.
Example Code
This sketch uses |= to enable LED and Serial flag bits.
const int LED_PIN = 2;
byte flags = 0b00000000;
const byte FLAG_LED_ENABLE = 0b00000001;
const byte FLAG_LOG_SERIAL = 0b00000100;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
flags |= FLAG_LED_ENABLE;
flags |= FLAG_LOG_SERIAL;
if (flags & FLAG_LED_ENABLE) {
digitalWrite(LED_PIN, HIGH);
}
if (flags & FLAG_LOG_SERIAL) {
Serial.print("flags = ");
Serial.println(flags, BIN);
}
delay(500);
}Example Code Explanation
flagsstarts as zero, so no feature bits are active.flags |= FLAG_LED_ENABLEsets bit 0 to enable LED behavior.flags |= FLAG_LOG_SERIALsets bit 2 for serial logging.- Bit checks confirm enabled features and run matching behavior.
- Serial output prints the flag value in binary so updates are visible.
What Happens Inside
- Current variable bits are loaded.
- Mask bits are loaded from the right side.
- Bitwise OR runs on each bit pair.
- Any bit with at least one 1 becomes 1 in result.
- Result is stored back into the same variable.
| Loop | flags before | operations | flags after |
|---|---|---|---|
| 1 | 00000000 | |=00000001, |=00000100 | 00000101 |
| 2 | 00000101 | |=00000001, |=00000100 | 00000101 |
Common Mistakes with |=
- Using logical
||instead of bitwise|/|=. - Expecting
|=to clear bits (it sets bits, does not clear them). - Using wrong mask values and enabling unintended bits.
- Not inspecting binary output when debugging flags.
Best Practices for |=
- Use named mask constants for readability.
- Use binary literals for teaching and debugging bit patterns.
- Print flags in binary to confirm which bits are set.
- Keep flag-setting and flag-checking logic near each other for clarity.
- Comment each mask purpose so future edits do not enable wrong bits.
Practice Task
- Add a new mask bit for a second LED feature and set it with
|=. - Print flags before and after each
|=update in binary format. - Start flags with
00000010and predict final value after both masks. - Compare behavior of
|=with long-formflags = flags | mask.
Try it now
Open the simulator workspace and observe how |= sets flag bits and enables LED/Serial behaviors.