Skip to content

Commit a941570

Browse files
authored
Update swap_all_odd_and_even_bits.py
1 parent afefd27 commit a941570

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed
Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
1-
def swap_odd_even_bits(num):
2-
# Get all even bits
3-
even_bits = (
4-
num & 0xAAAAAAAA
5-
) # 0xAAAAAAAA is a 32-bit number with all even bits set to 1
6-
7-
# Get all odd bits
8-
odd_bits = (
9-
num & 0x55555555
10-
) # 0x55555555 is a 32-bit number with all odd bits set to 1
11-
12-
# Right shift even bits by 1 and left shift odd bits by 1 to swap them
13-
even_bits >>= 1
14-
odd_bits <<= 1
15-
16-
# Combine the swapped even and odd bits
17-
result = even_bits | odd_bits
18-
19-
return result
20-
21-
22-
# Example usage:
23-
num = 23 # Binary: 10111
24-
swapped_num = swap_odd_even_bits(num) # Result: 43 (Binary: 101011)
25-
print(swapped_num)
26-
27-
# In this code:
28-
29-
# 1. We use bitwise AND operations to separate the even bits (0, 2, 4, 6, etc.) and odd bits (1, 3, 5, 7, etc.) in the input number.
30-
# 2. We then right-shift the even bits by 1 position and left-shift the odd bits by 1 position to swap them.
31-
# 3. Finally, we combine the swapped even and odd bits using a bitwise OR operation to obtain the final result.
1+
def show_bits(before: int, after: int) -> str:
2+
return f"{before}: {before:08b}\n{after}: {after:08b}"
3+
4+
5+
def swap_odd_even_bits(num: int) -> int:
6+
"""
7+
1. We use bitwise AND operations to separate the even bits (0, 2, 4, 6, etc.) and
8+
odd bits (1, 3, 5, 7, etc.) in the input number.
9+
2. We then right-shift the even bits by 1 position and left-shift the odd bits by
10+
1 position to swap them.
11+
3. Finally, we combine the swapped even and odd bits using a bitwise OR operation
12+
to obtain the final result.
13+
>>> print(show_bits(0, swap_odd_even_bits(0)))
14+
0: 00000000
15+
0: 00000000
16+
>>> print(show_bits(1, swap_odd_even_bits(1)))
17+
1: 00000001
18+
2: 00000010
19+
>>> print(show_bits(2, swap_odd_even_bits(2)))
20+
2: 00000010
21+
1: 00000001
22+
>>> print(show_bits(3, swap_odd_even_bits(3)))
23+
3: 00000011
24+
3: 00000011
25+
>>> print(show_bits(4, swap_odd_even_bits(4)))
26+
4: 00000100
27+
8: 00001000
28+
>>> print(show_bits(5, swap_odd_even_bits(5)))
29+
5: 00000101
30+
10: 00001010
31+
>>> print(show_bits(6, swap_odd_even_bits(6)))
32+
6: 00000110
33+
5: 00000101
34+
>>> print(show_bits(23, swap_odd_even_bits(23)))
35+
23: 00010111
36+
43: 00101011
37+
"""
38+
# Get all even bits - 0xAAAAAAAA is a 32-bit number with all even bits set to 1
39+
even_bits = num & 0xAAAAAAAA
40+
41+
# Get all odd bits - 0x55555555 is a 32-bit number with all odd bits set to 1
42+
odd_bits = num & 0x55555555
43+
44+
# Right shift even bits and left shift odd bits and swap them
45+
return even_bits >> 1 | odd_bits << 1
46+
47+
48+
if __name__ == "__main__":
49+
import doctest
50+
51+
doctest.testmod()
52+
for i in (-1, 0, 1, 2, 3, 4, 23, 24):
53+
print(show_bits(i, swap_odd_even_bits(i)), '\n')

0 commit comments

Comments
 (0)