basics · intermediate · ~15 min

Check all flags present

Test whether every masked bit is set.

Challenge

Implement:

int has_all_flags(unsigned v, unsigned mask);

Return 1 if every bit set in mask is also set in v, else 0.

Input format

A flags value v and a mask.

Output format

1 if all mask bits are present in v.

Constraints

An empty mask (0) is vacuously present -> 1.

Starter code

#include <stddef.h>
/* Return 1 if every bit set in mask is also set in v (all flags present). */
int has_all_flags(unsigned v,unsigned mask){ (void)v;(void)mask; return 0; }

Common mistakes

Testing v & mask != 0 (that is any flag, not all).

Edge cases to handle

Superset v returns 1; a single missing bit returns 0.

Background lessons

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