Bit Manipulation - Counting Bits (0 to N)
The following code attempts to count set bits for all numbers from 0 to n using Brian Kernighan's trick. Identify the line containing the subtle bug that causes incorrect results.
def countBits(n):
res = []
for i in range(1, n + 1):
count = 0
x = i
while x:
x &= x - 1
count += 1
res.append(count)
return res
