-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.py
45 lines (31 loc) · 840 Bytes
/
solution.py
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
44
45
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the flippingBits function below.
def flippingBits(n):
result = 0
#go from 31 to 0
for i in range(31,-1,-1):
#2 to the power gives us first bit
#if the bit is 1, flip it to zero and subtract it from original number
if n//(2**i) == 1:
n = n - 2**i
x = 0
#if bit is 0 flip it to 1
else:
x = 1
#each time update the result
result += 2**i * x
#return the final result
return result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
n = int(input())
result = flippingBits(n)
fptr.write(str(result) + '\n')
fptr.close()