forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle_kth_bit.py
More file actions
29 lines (23 loc) · 775 Bytes
/
toggle_kth_bit.py
File metadata and controls
29 lines (23 loc) · 775 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
# Python program to toggle the k-th bit of a number.
def toggle(num, k):
return (num ^ (1 << (k-1)))
if __name__ == '__main__':
print("Enter the number: ", end="")
n = int(input())
print("Enter the value of k(where you need to toggle the k'th bit): ", end="")
b = int(input())
res = toggle(n, b)
print("The given number, after toggling the k-th bit is {}".format(res))
"""
Time Complexity: O(1)
Space Complexity: O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE 1
Enter the number: 24
Enter the value of k(where you need to toggle the k'th bit): 3
The given number, after toggling the k-th bit is 28.
SAMPLE 2
Enter the number: 33
Enter the value of k(where you need to toggle the k'th bit): 12
The given number, after toggling the k-th bit is 2081.
"""