Algorithms




Pay Notebook Creator: Elaine Chan0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [2]:
#crosscompute
In [3]:
def count_bits (x):
    num_bits = 0
    while x:
        num_bits += x & 1
        x >>= 1
    return num_bits
In [6]:
count_bits(2)
Out[6]:
1
In [7]:
count_bits(10)
Out[7]:
2
In [8]:
count_bits(99)
Out[8]:
4
In [ ]: