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

  1. Convert both values into binary.
  2. Compare each bit pair position.
  3. Write 1 only when pair is 1&1.
  4. Write 0 for all other pairs.

Example: 0b10110010 & 0b00100000 = 0b00100000. This means that selected bit is ON.

Bitwise Truth Table

ABA & B
000
010
100
111

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.
OperatorInputTypical use
&Integer bitsMask/extract flags
&&Boolean expressionsif/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

  1. flags stores many ON/OFF states in one byte.
  2. flags & LED_ENABLE_MASK extracts LED bit.
  3. flags & SENSOR_READY_MASK extracts sensor-ready bit.
  4. flags & ERROR_MASK extracts error bit.
  5. LED behavior is controlled by the extracted LED bit result.
  6. Serial prints all extracted bits so behavior is easy to verify.

What Happens Inside

  1. CPU loads `flags` and mask values.
  2. ALU performs bitwise AND at each bit position.
  3. Only shared 1-bits survive in result.
  4. Result variable is tested as zero/non-zero in logic.
ExpressionBinary resultMeaning
flags & LED_ENABLE_MASK00000010LED enabled bit is ON
flags & SENSOR_READY_MASK00100000Sensor-ready bit is ON
flags & ERROR_MASK00010000Error 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 1 when 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) != 0 for 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.

Run in Simulator