Bitwise AND in C – Why 2 & 3 Is 2, Not 1

Bitwise AND works bit by bit, not value by value — and that’s exactly what this question from our C Programming Quiz App tests. If you compute 2 & 3 by gut feeling you’ll likely say 1 or 0; if you write out the binary, the answer falls out immediately.

The Quiz Question

int a = 2, b = 3;
printf("%d", a & b);

What is printed by this code?

  1. 1
  2. 2
  3. 3
  4. 0

The Correct Answer: 2

Write both operands in binary and AND each column — a result bit is 1 only where both inputs have a 1:

  2  =  1 0
  3  =  1 1
  ─────────  &
       1 0   =  2

The twos bit is set in both numbers, so it survives; the ones bit is set only in 3, so it’s cleared. Verified on gcc 13.3 and Apple clang 21, warning-free:

$ gcc -Wall -Wextra and.c && ./a.out
2

Why Each Wrong Answer Is Wrong

Why not 1?

1 is what the logical AND gives: 2 && 3 asks “are both values truthy?” and answers with a plain 1-or-0. We ran both in the same program:

printf("%d\n", a & b);    /* 2 — bitwise: combines the bits    */
printf("%d\n", a && b);   /* 1 — logical: true/false verdict   */

One ampersand operates on bits, two operate on truth values. Typo-ing one for the other compiles fine and silently computes the wrong thing — a classic C bug.

Why not 3?

3 (binary 11) is what bitwise OR gives: 2 | 3 keeps a bit if either operand has it. AND is the stricter operation — both must have the bit.

Why not 0?

0 would mean the numbers share no bits at all — true for, say, 2 & 5 (010 & 101 = 000), but 2 and 3 overlap on the twos bit. “AND of different numbers must be 0” is a false intuition worth unlearning.

What Bitwise AND Is Actually For

& is the masking operator — it extracts or tests specific bits:

if (n & 1)          /* odd/even test: is the ones bit set?      */
    printf("odd");

int low4 = n & 0xF; /* keep only the lowest 4 bits              */

if (flags & FLAG_READABLE)   /* is this option bit turned on?   */
    ...

The even/odd test n & 1 is the most famous: an odd number always has its lowest bit set, so ANDing with 1 isolates exactly that bit. Together with OR to set bits, XOR to toggle them, and shifts to position them, AND completes the bit-manipulation toolkit that flags, permissions, and embedded registers are built on.

Frequently Asked Questions

What is the difference between & and && in C?

& is bitwise AND: it combines the bits of two integers (2 & 3 = 2). && is logical AND: it evaluates truthiness and yields 1 or 0 (2 && 3 = 1), and it short-circuits — the right side isn’t evaluated if the left is false.

How do I check if a number is even using bitwise AND?

n & 1 isolates the lowest bit: 1 means odd, 0 means even. It’s equivalent to n % 2 for non-negative numbers, and compilers generate the same code for both.

How do I test whether a specific bit is set?

AND with a mask for that bit: if (n & (1 << k)) tests bit k. The result is nonzero exactly when the bit is 1.

Related Reading

Recommended Books

This question is #9 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>