basics · intermediate · ~15 min
Extract only the least-significant 1 bit.
Implement:
unsigned isolate_lowest_set(unsigned x);
Return the value of the lowest set bit of x (0 if x==0). E.g. 0b10100 -> 0b100.
x.
The lowest set bit's value, or 0.
x==0 returns 0.
#include <stddef.h>
/* Return the value of the lowest set bit of x (0 if x==0). e.g. 0b10100 -> 0b100. */
unsigned isolate_lowest_set(unsigned x){ (void)x; return 0; }
Sign issues if you compute -x on a signed type; use unsigned.
A power of two returns itself.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.