basics · beginner · ~15 min

Clear a set of flags

Turn off every bit in a mask.

Challenge

Implement:

unsigned clear_flags(unsigned v, unsigned mask);

Return v with every bit in mask cleared.

Input format

A flags value v and a mask.

Output format

v with the mask bits removed.

Constraints

Empty mask leaves v unchanged; full mask yields 0.

Starter code

#include <stddef.h>
/* Return v with every bit in mask cleared. */
unsigned clear_flags(unsigned v,unsigned mask){ (void)mask; return v; }

Common mistakes

Using v & mask (keeps only the mask bits) instead of v & ~mask.

Edge cases to handle

Bits outside the mask are preserved.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.