Quiz Entry - updated: 2026.07.14
Why does if (flags & MASK == 0) not work as expected?
Because == binds tighter than bitwise &, the compiler reads it as flags & (MASK == 0) — comparing MASK to 0 first, then ANDing — so you must write (flags & MASK) == 0.
// WRONG - compares MASK to 0 first!
// Parsed as: flags & (MASK == 0)
if (flags & MASK == 0)
// If MASK != 0, this becomes: flags & 0 = 0
// CORRECT - use parentheses
// Check if masked bits are all zero
if ((flags & MASK) == 0)
The precedence trap:
| Operator | Precedence |
|---|---|
== != |
7 |
& |
8 (lower!) |
^ |
9 (even lower!) |
| |
10 (lowest of bitwise) |
All bitwise operators have lower precedence than comparison operators!
// These are ALL wrong:
// Means: a & (b == c)
if (a & b == c)
// Means: a | (b != 0)
if (a | b != 0)
// Means: a ^ (b == c)
if (a ^ b == c)
// Correct versions:
if ((a & b) == c)
if ((a | b) != 0)
if ((a ^ b) == c)