Masks, Flags & Bit Manipulation
A mask is a value with ones where you care. Once you know the four mask idioms, flags and registers stop looking like magic.
In plain English
A mask is a stencil. It lets you paint or check only the bits you care about.
Mask idioms
Use AND to test a bit, OR to set it, AND with the complement to clear it, and XOR to toggle it. The mask has a 1 in the bit position you want to affect and zeros elsewhere. Shifting 1u by n creates a mask for bit n. Parenthesize the mask and the complement to keep precedence clear. Masks also support relational idioms: (x & MASK) != 0 tests whether any masked bit is set, and (x & MASK) == MASK tests whether every masked bit is set.
#define READ (1u << 0)
#define WRITE (1u << 1)
#define EXEC (1u << 2)
unsigned perms = READ | WRITE; /* set bits */
perms |= EXEC; /* add execute */
perms &= ~WRITE; /* clear write */
int can_read = (perms & READ) != 0;Flags, registers, and bitfields
Flags pack several boolean values into one integer. Permission bits, device registers, and network headers use the same idea. Always use unsigned types and fixed-width masks when the exact bit layout matters. Be careful with sign extension and with shifts whose count is greater than or equal to the width of the type.
#define FLAG_A (1u << 0)
#define FLAG_B (1u << 1)
#define FLAG_C (1u << 2)
unsigned flags = 0;
flags |= FLAG_A | FLAG_C;
if (flags & FLAG_A) {
printf("A is set\n");
}Relational mask idioms
A mask expression becomes a relational test when you compare the masked value. (x & MASK) != 0 is true when any masked bit is set. (x & MASK) == MASK is true only when every masked bit is set. (x & MASK) == 0 is true when none of the masked bits are set. Parenthesize the mask expression before comparing, because equality has lower precedence than bitwise AND in some expressions and higher in others.
#define READ (1u << 0)
#define WRITE (1u << 1)
#define EXEC (1u << 2)
unsigned perms = READ | WRITE;
if ((perms & READ) != 0) {
printf("read allowed\n");
}
if ((perms & (READ | WRITE)) == (READ | WRITE)) {
printf("read and write allowed\n");
}
if ((perms & EXEC) == 0) {
printf("execute denied\n");
}Watch it happen
Step through the memory changes one instruction at a time.
step-by-step
Step 1 of 6
Start with no permissions
perms = 0. No flag is set.
Key takeaways
- OR sets, AND clears, XOR toggles, AND tests.
- 1u << n creates a mask for bit n.
- Flags pack multiple booleans into one integer.
- Use unsigned types and parenthesize masks.
- (x & MASK) != 0 tests any bit; (x & MASK) == MASK tests all bits.
Common pitfalls
- Using & instead of && in a boolean context.
- Forgetting parentheses around a mask or complement.
- Shifting by the width of the type.
- Testing a single bit when the mask contains several bits.
Key focus
- Know the idioms for setting, clearing, toggling, and testing a bit.
- Know the difference between testing any masked bit and testing all masked bits.
- Expect questions on mask expressions and precedence.
Quick check
1. Which expression sets bit 2 of x?
2. Which expression clears bit 3 of x?