mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 17:01:28 -04:00
play with bits
This commit is contained in:
parent
c77eb0dd00
commit
4b22537ee7
5 changed files with 73 additions and 0 deletions
12
bit_manipulation/count_1s.py
Normal file
12
bit_manipulation/count_1s.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
def count_ones(n: int) -> int:
|
||||
|
||||
counter = 0
|
||||
|
||||
while n:
|
||||
|
||||
if n & 1:
|
||||
counter += 1
|
||||
|
||||
n >>= 1
|
||||
|
||||
return counter
|
Loading…
Add table
Add a link
Reference in a new issue