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
- Read current value bits from the left variable.
- Read mask bits from the right side.
- Apply bitwise AND for each bit position.
- Store final bits back into the left variable.
Rule reminder: only 1 & 1 produces 1. All other bit pairs produce 0.
| Bit A | Bit B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Updating Variables with &=
flags &= 0b00001111keeps lower 4 bits only.state &= ENABLE_MASKkeeps allowed state bits.value &= 0xFFlimits value to lowest 8 bits.
&= vs & and = (Comparison)
x = x & maskandx &= maskare equivalent.&=is shorter and clearer inside repeated updates.
x = x & mask; // long form
x &= mask; // compound formUsing &= with Bit Masks
Bit masks are patterns that tell which bits to keep (mask bit 1) and which bits to clear (mask bit 0).
| Step | Original flags | Mask | Result after &= |
|---|---|---|---|
| Keep bit 0 only | 00000111 | 00000001 | 00000001 |
| Keep low nibble | 10110110 | 00001111 | 00000110 |
Data Type Notes
- Use integer types (
byte,int,unsigned int) with bitwise operators. byteis 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
flagsstarts with three low bits set (00000111).flags &= KEEP_LED_BITkeeps only bit 0 and clears others.flags & KEEP_LED_BITchecks if kept bit is still active.- If active, LED turns ON and Serial prints kept-bit message.
- If not active, LED turns OFF and Serial prints cleared-bit message.
What Happens Inside
- CPU loads left variable bits and mask bits.
- Bitwise AND runs per bit position.
- Bits paired with mask 0 are cleared to 0.
- Bits paired with mask 1 keep original bit values.
- Final bit result is written back to the same variable.
| Loop | flags before | mask | flags after &= |
|---|---|---|---|
| 1 | 00000111 | 00000001 | 00000001 |
| 2 | 00000001 | 00000001 | 00000001 |
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_BITfor 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
- Change mask to
0b00000011and observe which bits remain. - Print
flagsas binary before and after&=. - Add another flag bit and design a mask that keeps exactly two bits.
- 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.