master-algorithms-py/bit_manipulation/count_1s.py
2023-07-29 20:18:30 -07:00

12 lines
215 B
Python

def count_ones(n: int) -> int:
counter = 0
while n:
if n & 1:
counter += 1
n >>= 1
return counter