mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-06 22:28:49 -04:00
cleaning up and organizing old problems (builtin)
This commit is contained in:
parent
6afe96fa4d
commit
3fdbc2a605
106 changed files with 480 additions and 1472 deletions
25
src/bitwise/find_bit_len.py
Executable file
25
src/bitwise/find_bit_len.py
Executable file
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Find how many bits a int has:
|
||||
1) Start with a mask of 1
|
||||
2) Mask with AND
|
||||
3) if result (if true): count += 1
|
||||
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int))
|
||||
'''
|
||||
|
||||
|
||||
def find_bit_len(int_num):
|
||||
lenght = 0
|
||||
while int_num:
|
||||
int_num >>= 1
|
||||
lenght += 1
|
||||
return lenght
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for i in range(17):
|
||||
print(find_bit_len(i))
|
||||
print i.bit_length()
|
||||
print
|
Loading…
Add table
Add a link
Reference in a new issue