basics · intermediate · ~15 min

Isolate the lowest set bit

Extract only the least-significant 1 bit.

Challenge

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.

Input format

x.

Output format

The lowest set bit's value, or 0.

Constraints

x==0 returns 0.

Starter code

#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; }

Common mistakes

Sign issues if you compute -x on a signed type; use unsigned.

Edge cases to handle

A power of two returns itself.

Background lessons

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