forked from prosoftwaredevelopment/CompetetiveCodingPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmagicPART1.py
More file actions
43 lines (35 loc) · 724 Bytes
/
bitmagicPART1.py
File metadata and controls
43 lines (35 loc) · 724 Bytes
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
40
41
42
43
# ispowerof 2
# n -> input
# True/False -> output
# check if n is a powerof 2
# 512 -> True 512 = 2**9
# 1024 -> True 1024 = 2**10
def ispowerof2(n):
# T.C = O(1)
if n <= 0:
return False
x = n
y = not(n & (n-1))
return x and y
# return's number of 1's in binary representation of int
# 5 -> 101 ans = 2
# 7 -> 111 ans = 3
def bruteforcecntbits(n):
# T.C = O(n)
s = str(bin(n))[2:]
print("{}".format(s))
return s.count('1')
def cntbits(n):
# T.C = O(logn)
cnt = 0
while n:
cnt+=1
n = n & (n-1)
return cnt
t= int(input())
while t:
n = int(input())
#print(ispowerof2(n))
print(bruteforcecntbits(n))
print(cntbits(n))
t=t-1