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

  1. Read current bits from the variable.
  2. Read mask bits from the right side.
  3. Apply bitwise OR at each bit position.
  4. Store resulting bits back into the same variable.

Rule reminder: if either bit is 1, the result bit becomes 1.

Bit ABit BA | B
000
011
101
111

Updating Variables with |=

  • flags |= 0b00000001 sets bit 0.
  • flags |= 0b00000100 sets bit 2 without clearing others.
  • config |= MODE_MASK enables selected mode bits.

|= vs | and = (Comparison)

  • x = x | mask and x |= mask are equivalent.
  • |= is shorter and clearer in repeated flag updates.
x = x | mask;   // long form
x |= mask;      // compound form

Using |= with Bit Masks

Use mask bits with 1 where you want to set a bit. Bits with 0 in the mask stay unchanged.

StepOriginal flagsMaskResult after |=
Enable LED000000000000000100000001
Enable logging000000010000010000000101

Data Type Notes

  • Use integer types for bitwise operators (byte, int).
  • byte is 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

  1. flags starts as zero, so no feature bits are active.
  2. flags |= FLAG_LED_ENABLE sets bit 0 to enable LED behavior.
  3. flags |= FLAG_LOG_SERIAL sets bit 2 for serial logging.
  4. Bit checks confirm enabled features and run matching behavior.
  5. Serial output prints the flag value in binary so updates are visible.

What Happens Inside

  1. Current variable bits are loaded.
  2. Mask bits are loaded from the right side.
  3. Bitwise OR runs on each bit pair.
  4. Any bit with at least one 1 becomes 1 in result.
  5. Result is stored back into the same variable.
Loopflags beforeoperationsflags after
100000000|=00000001, |=0000010000000101
200000101|=00000001, |=0000010000000101

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

  1. Add a new mask bit for a second LED feature and set it with |=.
  2. Print flags before and after each |= update in binary format.
  3. Start flags with 00000010 and predict final value after both masks.
  4. Compare behavior of |= with long-form flags = flags | mask.

Try it now

Open the simulator workspace and observe how |= sets flag bits and enables LED/Serial behaviors.

Run in Simulator