Skip to content

Latest commit

 

History

History
42 lines (21 loc) · 610 Bytes

Bit_Counting.md

File metadata and controls

42 lines (21 loc) · 610 Bytes

CodeWars Python Solutions


Bit Counting

Definition

Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

Example

The binary representation of 1234 is 10011010010, so the function should return 5 in this case


Given Code

def countBits(n):
    pass

Solution

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

See on CodeWars.com