basics · beginner · ~15 min
Force a single bit to 0.
Implement:
unsigned clear_bit(unsigned x, int i);
Return x with bit i set to 0. i is in 0..31.
x and a bit index i.
x with bit i = 0.
i in 0..31.
#include <stddef.h>
/* Return x with bit i set to 0. i in 0..31. */
unsigned clear_bit(unsigned x,int i){ (void)i; return x; }
Forgetting to invert the mask (& mask clears everything else instead).
Clearing an already-clear bit is a no-op.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.