basics · beginner · ~15 min

Isolate the lowest set bit

Use the x & -x idiom to isolate the lowest set bit.

Challenge

Implement unsigned lowest_set_bit(unsigned x) returning the value of the least-significant set bit of x (e.g. 12 -> 4). Return 0 when x is 0.

Starter code

unsigned lowest_set_bit(unsigned x) {
    /* TODO */
    return 0;
}

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