-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_45.py
33 lines (21 loc) · 892 Bytes
/
Day_45.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
"""
DAY 45 : Swap all odd and even bits.
https://www.geeksforgeeks.org/swap-all-odd-and-even-bits/
QUESTION : Given an unsigned integer N. The task is to swap all odd bits with even bits.
For example, if the given number is 23 (00010111), it should be converted to 43(00101011).
Here, every even position bit is swapped with adjacent bit on the right side(even position
bits are highlighted in the binary representation of 23), and every odd position bit is
swapped with an adjacent on the left side.
Expected Time Complexity: O(1).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^9
"""
def swapper(x):
even_bits = x & 0xAAAAAAAA
odd_bits = x & 0x55555555
even_bits >>= 1
odd_bits <<= 1
return (even_bits | odd_bits)
num = int(input('Enter number: '))
print('After swapping: ', swapper(num))