mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 08:51:31 -04:00
👾
This commit is contained in:
parent
1d44d182e2
commit
a85ed914d3
320 changed files with 0 additions and 0 deletions
17
bit_operations/count_1s.py
Normal file
17
bit_operations/count_1s.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
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