Bit Manipulation - Counting Bits (0 to N)
Consider the following Python code that uses Brian Kernighan's bit counting trick to count set bits from 0 to n. What is the output when n = 3?
def countBits(n):
res = []
for i in range(n + 1):
count = 0
x = i
while x:
x &= x - 1
count += 1
res.append(count)
return res
print(countBits(3))
