Bitwise AND

In short: The bitwise AND operator (&) compares two numbers bit by bit, setting each result bit to 1 only when both input bits are 1. It's commonly used to mask out bits, test whether a specific bit is set, and check if a number is even or odd.

The AND operation takes two bits and returns 1 if both bits are 1. Otherwise, it returns 0.

1 & 1 → 1 1 & 0 → 0 0 & 1 → 0 0 & 0 → 0

Think of it like a hose with two knobs. Both knobs must be set to on for water to come out.

When performing AND on two integers, the AND operation is calculated on each pair of bits (the two bits at the same index in each number).

5 & 6 # gives 4 # At the bit level: # 0101 (5) # & 0110 (6) # = 0100 (4)

Frequently Asked Questions

What does the bitwise AND operator do?

It compares two numbers bit by bit and produces a 1 in each position only where both numbers have a 1; otherwise the result bit is 0.

What is bitwise AND used for?

Masking (keeping only certain bits), testing whether a particular bit is set, and checking if a number is even with n & 1 == 0.

What's the difference between & and &&?

& is the bitwise AND that operates on the individual bits of integers; && is the logical AND that operates on boolean values and short-circuits.

Last updated: June 17, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .