basics · beginner · ~15 min
Turn off every bit in a mask.
Implement:
unsigned clear_flags(unsigned v, unsigned mask);
Return v with every bit in mask cleared.
A flags value v and a mask.
v with the mask bits removed.
Empty mask leaves v unchanged; full mask yields 0.
#include <stddef.h>
/* Return v with every bit in mask cleared. */
unsigned clear_flags(unsigned v,unsigned mask){ (void)mask; return v; }
Using v & mask (keeps only the mask bits) instead of v & ~mask.
Bits outside the mask are preserved.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.