basics · beginner · ~15 min
Force a single bit to 1.
Implement:
unsigned set_bit(unsigned x, int i);
Return x with bit i set to 1 (idempotent). i is in 0..31.
x and a bit index i.
x with bit i = 1.
i in 0..31.
#include <stddef.h>
/* Return x with bit i set to 1. i in 0..31. */
unsigned set_bit(unsigned x,int i){ (void)i; return x; }
Toggling instead of setting (breaks when the bit was already 1).
Setting an already-set bit is a no-op.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.