Operators
Lesson 56: Using & Bitwise AND Operator
Learn how & compares bits, extracts masked flags, and supports low-level embedded control logic.
Progress indicator
Lesson 56 of 57
Learning Objectives
- Understand what bitwise AND (&) does at bit level.
- Read the truth table and predict output bits confidently.
- Use bit masks to extract specific status bits from packed values.
- Understand the difference between & and && in practical code.
- Avoid common mask, precedence, and readability mistakes.
Concept Explanation
What is the Bitwise AND Operator (&)
The & operator compares two numbers bit-by-bit.
A result bit becomes 1 only if both compared bits are 1.
Bitwise AND Syntax
result = valueA & valueB;
byte selected = flags & MASK;How & Works
- Convert both values into binary.
- Compare each bit pair position.
- Write 1 only when pair is 1&1.
- Write 0 for all other pairs.
Example: 0b10110010 & 0b00100000 = 0b00100000. This means that selected bit is ON.
Bitwise Truth Table
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Using & with Bit Masks
A mask keeps only selected bit positions and clears the rest.
byte flags = 0b10110010;
byte ledEnabled = flags & 0b00000010;If result is non-zero, selected bit is active. If result is zero, selected bit is inactive.
& vs && (Comparison)
&works on bits inside integer values.&&works on true/false conditions.- Use
&for masks and&&for logical branching.
| Operator | Input | Typical use |
|---|---|---|
& | Integer bits | Mask/extract flags |
&& | Boolean expressions | if/else condition flow |
When to Use &
- Reading status flags from registers or sensor bytes.
- Checking specific bits in packed protocol data.
- Controlling features based on bit fields.
One byte can store many states such as LED enabled, sensor ready, and error flag.
Example Code
This sketch reads multiple flags using masks and controls the built-in LED.
const int LED_PIN = 2;
byte flags = 0b10110010;
const byte LED_ENABLE_MASK = 0b00000010;
const byte SENSOR_READY_MASK = 0b00100000;
const byte ERROR_MASK = 0b00010000;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
byte ledEnabled = flags & LED_ENABLE_MASK;
byte sensorReady = flags & SENSOR_READY_MASK;
byte errorState = flags & ERROR_MASK;
if (ledEnabled) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
Serial.print("flags=");
Serial.print(flags, BIN);
Serial.print(", ledEnabled=");
Serial.print(ledEnabled ? 1 : 0);
Serial.print(", sensorReady=");
Serial.print(sensorReady ? 1 : 0);
Serial.print(", errorState=");
Serial.println(errorState ? 1 : 0);
delay(700);
}Example Code Explanation
flagsstores many ON/OFF states in one byte.flags & LED_ENABLE_MASKextracts LED bit.flags & SENSOR_READY_MASKextracts sensor-ready bit.flags & ERROR_MASKextracts error bit.- LED behavior is controlled by the extracted LED bit result.
- Serial prints all extracted bits so behavior is easy to verify.
What Happens Inside
- CPU loads `flags` and mask values.
- ALU performs bitwise AND at each bit position.
- Only shared 1-bits survive in result.
- Result variable is tested as zero/non-zero in logic.
| Expression | Binary result | Meaning |
|---|---|---|
flags & LED_ENABLE_MASK | 00000010 | LED enabled bit is ON |
flags & SENSOR_READY_MASK | 00100000 | Sensor-ready bit is ON |
flags & ERROR_MASK | 00010000 | Error bit is ON |
Common Mistakes with &
- Using
&&where bitwise&is needed. - Using incorrect mask values for target bit positions.
- Forgetting parentheses in combined expressions.
- Comparing masked output to
1when mask is not bit-0.
Best Practices for &
- Define named masks such as
LED_ENABLE_MASK. - Use binary literals for easier bit-position reading.
- Use checks like
(flags & MASK) != 0for clarity. - Print binary values while debugging or teaching bit logic.
Try it now
Open the simulator workspace and inspect how masks extract active status bits.