Ones in Binary — Solution & Editorial
Repeatedly clear the lowest set bit with n &= n-1, or use the built-in popcount.
Complexity: O(number of set bits)
Repeatedly clear the lowest set bit with n &= n-1, or use the built-in popcount.
Complexity: O(number of set bits)