write a function that takes an unsigned integer and returns the number of 1 bits it has also known as the hamming weight for example the 32bit integer 11 has binary representation 00000000000000000000000000001011 so the function should return 3 tn ok k is the number of 1s present in binary representation note this complexity is better than olog n e g for n 00010100000000000000000000000000 only 2 iterations are required number of loops is equal to the number of 1s in the binary representation def countonesrecurn using brian kernighan s algorithm iterative approach count 0 while n n n1 count 1 return count using brian kernighan s algorithm recursive approach using brian kernighan s algorithm iterative approach
def count_ones_recur(n):
    

    if not n:
        return 0
    return 1 + count_ones_recur(n & (n-1))


def count_ones_iter(n):
    

    count = 0
    while n:
        n &= (n-1)
        count += 1
    return count