def check_pow_2(num: int) -> int:
if num == 0:
return 0
if num & (num - 1):
return 1;
return -1;
switch = {
0: "number is 0",
1: "number is power of 2",
-1: "number is not power of 2 nor 0"
}
case = check_pow_2(7)
print(switch[case])
import time
def count_bits_tobe_flipped(a: int, b: int) -> int:
x = a ^ b
counter = 0
while x != 0:
if x & 1:
counter += 1
x >>= 1
return counter
st = time.time()
print(count_bits_tobe_flipped(63, 20))
et = time.time()
elapsed_time = (et - st) * 1000
print('Execution time:', elapsed_time, 'milliseconds')
def count_bits_flip(a, b):
c = a ^ b
count = 0
while c != 0:
count += 1
c &= (c-1)
return count
st = time.time()
print(count_bits_flip(63, 20))
et = time.time()
elapsed_time = (et - st) * 1000
print('Execution time:', elapsed_time, 'milliseconds')