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
def count_ones_recur(n):
    """Using Brian Kernighan's Algorithm. (Recursive Approach)"""

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


def count_ones_iter(n):
    """Using Brian Kernighan's Algorithm. (Iterative Approach)"""

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