I found the following codes in a book:
def count_bits(x):
num_bits = 0
while x:
num_bits += x&1
x >>= 1
return num_bits
print(count_bits(12))
I don't understand this line (num_bits += x&1)
Let's say I input 12 (1100), the first character ("1") gets counted. But then there is a right shift and 1100 becomes 0110. If the counter moves to the second character, won't 1 be counted twice?