Count number of ones in binary number

#COUNT NUMBER OF ONES IN A BINARY NUMBER

def CountBits(n):
    if n==0:
        return 0
    elif n%2==0:
        return CountBits(n//2)
    else:
        return 1-CountBits(n//2)
       
num = int(input("Enter a decimal number : "))
print("Total number of ones in binary number of",num,"is",CountBits(num))

Comments