Algorithms and Data Structures · Middle · Question 62 of 75
What does x & (x - 1) do for a positive integer x?
- 1Returns how many bits are set in the number
- 2Shifts the number one bit to the left
- ✓Correct answer: Clears the lowest set bit (0 only for powers of two)
Explanation
Subtracting 1 flips the lowest 1 and the zeros below it; AND removes that bit. So x & (x - 1) === 0 checks for a power of two, and repeating it counts the set bits.