Operators

Lesson 49: Using &= Compound Bitwise AND Operator

Learn how &= keeps selected bits and clears others, useful for flags, masks, and register-style control.

Progress indicator

Lesson 49 of 57

Learning Objectives

  • Understand what the compound bitwise AND operator (&=) does.
  • Use &= to keep selected bits and clear unwanted bits.
  • Compare &= with writing & and = separately.
  • Apply &= with bit masks in embedded-style flag handling.
  • Avoid common mistakes when mixing bitwise and logical operators.
  • Read bit-pattern transitions and predict final flag values.

Concept Explanation

What is the Compound Bitwise AND Operator (&=)

The &= operator applies bitwise AND to a variable and stores the result back into that same variable.

It is a short form of value = value & mask.

Compound Bitwise AND Syntax

flags &= mask;
registerValue &= 0b00001111;

How &= Works

  1. Read current value bits from the left variable.
  2. Read mask bits from the right side.
  3. Apply bitwise AND for each bit position.
  4. Store final bits back into the left variable.

Rule reminder: only 1 & 1 produces 1. All other bit pairs produce 0.

Bit ABit BA & B
000
010
100
111

Updating Variables with &=

  • flags &= 0b00001111 keeps lower 4 bits only.
  • state &= ENABLE_MASK keeps allowed state bits.
  • value &= 0xFF limits value to lowest 8 bits.

&= vs & and = (Comparison)

  • x = x & mask and x &= mask are equivalent.
  • &= is shorter and clearer inside repeated updates.
x = x & mask;   // long form
x &= mask;      // compound form

Using &= with Bit Masks

Bit masks are patterns that tell which bits to keep (mask bit 1) and which bits to clear (mask bit 0).

StepOriginal flagsMaskResult after &=
Keep bit 0 only000001110000000100000001
Keep low nibble101101100000111100000110

Data Type Notes

  • Use integer types (byte, int, unsigned int) with bitwise operators.
  • byte is beginner-friendly for 8-bit flag demonstrations.
  • Choose mask width to match the variable width for predictable results.

When to Use &=

  • Clear specific bits while preserving selected bits.
  • Apply safety masks before using flags in logic.
  • Work with low-level register-like values.

Example Code

This sketch uses &= with a mask to keep only the LED flag bit.

const int LED_PIN = 2;

byte flags = 0b00000111;
const byte KEEP_LED_BIT = 0b00000001;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  flags &= KEEP_LED_BIT;

  if (flags & KEEP_LED_BIT) {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("LED bit kept");
  } else {
    digitalWrite(LED_PIN, LOW);
    Serial.println("LED bit cleared");
  }

  delay(600);
}

Example Code Explanation

  1. flags starts with three low bits set (00000111).
  2. flags &= KEEP_LED_BIT keeps only bit 0 and clears others.
  3. flags & KEEP_LED_BIT checks if kept bit is still active.
  4. If active, LED turns ON and Serial prints kept-bit message.
  5. If not active, LED turns OFF and Serial prints cleared-bit message.

What Happens Inside

  1. CPU loads left variable bits and mask bits.
  2. Bitwise AND runs per bit position.
  3. Bits paired with mask 0 are cleared to 0.
  4. Bits paired with mask 1 keep original bit values.
  5. Final bit result is written back to the same variable.
Loopflags beforemaskflags after &=
1000001110000000100000001
2000000010000000100000001

Common Mistakes with &=

  • Using logical && instead of bitwise & or &=.
  • Using the wrong mask and clearing bits unintentionally.
  • Assuming &= sets bits; it mostly clears unwanted bits.
  • Not printing/debugging bit patterns during first tests.

Best Practices for &=

  • Define named masks like KEEP_LED_BIT for readability.
  • Use binary literals when teaching or debugging bit logic.
  • Log values in Serial to confirm mask behavior before integration.
  • Keep bit operations in small helper steps for beginner clarity.
  • Document mask meaning near declarations to avoid wrong-bit bugs.

Practice Task

  1. Change mask to 0b00000011 and observe which bits remain.
  2. Print flags as binary before and after &=.
  3. Add another flag bit and design a mask that keeps exactly two bits.
  4. Try clearing only bit 0 by combining other masks and compare behavior.

Try it now

Open the simulator workspace and observe how &= masks bits to keep only selected flags.

Run in Simulator