easy
Bit Counting Tricks
Implement three utilities on a non-negative 32-bit integer: count its 1 bits, decide whether it is a power of two, and isolate its lowest set bit. Each should run in time proportional to the number of set bits or better.
Constraints
- 0 ≤ x < 2^32
Examples
in: x = 12 (1100b)
out: popcount = 2, isPowerOfTwo = false, lowestBit = 4
in: x = 16
out: popcount = 1, isPowerOfTwo = true, lowestBit = 16
Code it yourself
Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.