-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.py
More file actions
39 lines (34 loc) · 1.09 KB
/
binary.py
File metadata and controls
39 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# For any number, we can check whether its ‘i’th bit is 0(OFF) or 1(ON) by bitwise
# ANDing it with “2^i” (2 raise to i).
#
# 1) Let us take number 'NUM' and we want to check whether it's 0th bit is ON or OFF
# bit = 2 ^ 0 (0th bit)
# if NUM & bit == 1 means 0th bit is ON else 0th bit is OFF
#
# 2) Similarly if we want to check whether 5th bit is ON or OFF
# bit = 2 ^ 5 (5th bit)
# if NUM & bit == 1 means its 5th bit is ON else 5th bit is OFF.
#
from copy import copy
import operator
def main(my_num):
bit_list = []
if my_num != 0:
while my_num >= 1:
if (my_num % 2) == 0:
bit_list.insert(0, '0')
my_num = my_num // 2
else:
bit_list.insert(0, '1')
my_num = (my_num - 1) // 2
else:
bit_list = ['0']
bit_string = ''.join(bit_list)
print(f"bit_string = {bit_string}")
if __name__ == "__main__":
input_int = int(input())
main(input_int)
'''
I tried using the '&' operator and it did not work the way it was supposed to:
Since it did not work, I used % 2
'''