- How the
&operator computes the bitwise AND of two integers - How binary representation matters for bitwise operations
- The bit-by-bit truth table:
1 & 1 = 1, everything else is0 - How to use AND as a mask to extract specific bits
- How
n & 1checks even/odd in a single operation
The bitwise AND operator
The bitwise AND operator & compares two integers bit by bit. For each bit position, if both corresponding bits are 1, the result bit is 1. Otherwise, the result bit is 0. That's the whole rule — but its applications are numerous.
Don't confuse & (bitwise AND) with && (logical AND). & operates on each bit of two integers and produces an integer result. && operates on two boolean values (treating nonzero as true) and produces 0 or 1. They look similar but are completely different operators.
Common uses of &:
- Masking — extract specific bits:
n & 0x0Fgives the low 4 bits ofn. - Even/odd check —
n & 1is 1 for odd, 0 for even. - Testing a bit —
n & (1 << k)is nonzero if bitkis set. - Clearing bits —
n & ~(1 << k)clears bitk. - Parity —
n & 1tells you if the least significant bit is set.
a & b sets each bit of the result to 1 only where both a and b have a 1. It's the "keep only the bits that are on in both" operator.
Truth table
For each pair of bits, the AND result is:
| Bit a | Bit b | a & b |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
In words: the result is 1 only when both input bits are 1. Otherwise it's 0. Applying this rule at every bit position gives the bitwise AND.
For example, 12 & 10:
Reading bit by bit from left to right:
- Bit 3 (8's place): 1 & 1 = 1
- Bit 2 (4's place): 1 & 0 = 0
- Bit 1 (2's place): 0 & 1 = 0
- Bit 0 (1's place): 0 & 0 = 0
Result: 1000 in binary = 8 in decimal. So 12 & 10 = 8.
Complete C Program
Here is the program — bitwise AND on two integers:
Program: Bitwise AND of two integers
#include <stdio.h>
int main(void) {
int a, b, result;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
result = a & b;
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("a & b = %d\n", result);
return 0;
}
A variant that also shows the binary representation:
Variant: Show binary representation
#include <stdio.h>
void print_binary(int n) {
for (int i = 7; i >= 0; i--) {
printf("%d", (n >> i) & 1);
if (i % 4 == 0 && i != 0) printf(" ");
}
printf("\n");
}
int main(void) {
int a, b, result;
printf("Enter two integers (0-255): ");
scanf("%d %d", &a, &b);
result = a & b;
printf("\na = ");
print_binary(a);
printf("b = ");
print_binary(b);
printf("a & b = ");
print_binary(result);
printf("\nDecimal result: %d\n", result);
return 0;
}
The print_binary helper iterates from bit 7 down to bit 0, using (n >> i) & 1 to isolate each bit — the AND operator appears here twice (once to compute the result, once to extract bits for display). This is the standard way to display binary in C, since printf has no %b specifier.
Program Output
Sample run of the basic program:
Enter two integers: 12 10
a = 12
b = 10
a & b = 8
As computed above: 12 (1100) & 10 (1010) = 1000 (8). Correct.
More examples:
Enter two integers: 255 15
a = 255
b = 15
a & b = 15
255 = 11111111, 15 = 00001111, AND is 00001111 = 15 — the mask kept only the low 4 bits of 255, which happen to be all 1s. And:
Enter two integers: 100 7
a = 100
b = 7
a & b = 4
100 = 1100100, 7 = 0000111, AND is 0000100 = 4. The low 3 bits of 100 are 100 (binary), which equals 4 in decimal.
The variant with binary display:
Enter two integers (0-255): 12 10
a = 0000 1100
b = 0000 1010
a & b = 0000 1000
Decimal result: 8
The binary view makes the operation obvious: the only bit where both 12 and 10 have a 1 is bit 3 (the 8's place), so that's the only 1 in the result.
Step-by-Step Explanation
1. Declarations: int a, b, result; — bitwise AND works on integers (or any integer type: char, short, long). The result has the same type as the operands after the usual arithmetic promotions.
2. Input: scanf("%d %d", &a, &b) reads two integers. The & here is not the bitwise operator — it's the address-of operator. Same symbol, completely different meaning, distinguished by context.
3. The AND operation: result = a & b computes the bitwise AND. Each bit of result is set to 1 only if the corresponding bit is 1 in both a and b. For 12 & 10: 1100 & 1010 = 1000 = 8.
4. Why is it useful? The AND operator is the primary tool for masking — extracting or testing specific bits. For example:
n & 1— extracts the lowest bit (0 if even, 1 if odd).n & 0xFF— extracts the low 8 bits (useful for extracting a byte from a word).n & (1 << k)— tests whether bitkis set (result is 0 or2k).n & ~0xF— clears the low 4 bits ofn.
5. Output format: %d prints the integer result in decimal. To see the binary, you'd need a custom function (as in the variant) or the compiler's binary literal syntax (C23's 0b1100, but that's input format only, not output).
6. Precedence: The bitwise AND has lower precedence than equality, relational, and arithmetic operators. So a & b == c means a & (b == c), not (a & b) == c — a classic source of bugs. Always parenthesize: (a & b) == c.
a & b is bit-by-bit: 1 only when both bits are 1. It's the mask operator — use it to extract bits, test flags, or clear specific bits. Watch out for & vs && and for precedence with ==.
Frequently Asked Questions
1. What's the difference between & and &&?
& is bitwise AND — it operates on each bit of two integers and produces an integer. && is logical AND — it treats its operands as booleans (0 = false, nonzero = true) and produces 0 or 1. For example, 5 & 3 = 1 (bitwise), but 5 && 3 = 1 (both nonzero, so result is true = 1). And 4 & 2 = 0 (bitwise, no common bits), but 4 && 2 = 1 (both nonzero). Use & for masking, && for boolean logic.
2. How do I check if a bit is set?
Use n & (1 << k). If the result is nonzero, bit k is set. For example, to test bit 3 (the 8's place): if (n & (1 << 3)). To check whether a number is even: if ((n & 1) == 0). To check odd: if (n & 1). These are common idioms that avoid the (usually slower) modulo operator.
3. How does n & 1 check even/odd?
Because in binary, the last bit of a number is 1 for odd numbers and 0 for even numbers. n & 1 isolates that last bit: it returns 1 if n is odd, 0 if n is even. This works for positive and negative numbers (two's complement), and it's faster than n % 2 on most CPUs. Note: n & 1 returns 1 for negative odd numbers too, while n % 2 returns −1 for negative odd numbers — another reason to prefer &.
4. Why is a & b == c often a bug?
Because == has higher precedence than &. So a & b == c parses as a & (b == c), not (a & b) == c. Since b == c evaluates to 0 or 1, the result is usually 0 (unless a has bit 0 set). If you meant "the AND equals c", write (a & b) == c. Better yet, always parenthesize bitwise expressions.
5. Can I use & on negative numbers?
Yes — the operator works on the binary representation of negative numbers (which is two's complement on all modern systems). For example, −1 & 5 = 5, because −1 is all 1s in binary, and x & all-1s = x. −2 & 3 = 2 (...11111110 & 00000011 = 00000010). The results depend on the bit patterns, which for negatives are implementation-defined (but in practice two's complement, uniform across platforms).
💡 Tip: once this works, try these classic AND applications: (a) extract specific bits with a mask (n & 0x0F for low 4 bits), (b) clear bits with AND-not (n & ~(1 << k)), (c) check multiple flags at once (if (flags & (FLAG_A | FLAG_B))), (d) implement a fast modulo for powers of 2 (n & (m - 1) instead of n % m when m is a power of 2). Each is a small extension of the same operator.