###Description:

Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number.

The binary representation of 1234 is 10011010010, so the function should return 5 in this case. Because there are five 1” or bits that are set in 10011010010.

###Solution:

def countBits(n):
    return bin(n).count("1")

Source:

CodeWars

January 1, 2016 Python


Previous post
With Python pick random item If you have a list of items or a sequence and you want to pic a random item from said sequence. Here is a quick way to do it
Next post
Python Code Execution Process Bytecode Generation and Python Virtual Machine are two videos that do a good job of giving a brief and clear explanation