Skip to content

Commit 5346623

Browse files
Solve : Reverse Bits
1 parent cdf1427 commit 5346623

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

reverse-bits/printjin-gmailcom.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Solution:
22
def reverseBits(self, n):
3-
b = bin(n)[2:]
4-
b = b.zfill(32)
5-
reversed_b = b[::-1]
6-
return int(reversed_b, 2)
3+
n = (n >> 16) | (n << 16)
4+
n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8)
5+
n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4)
6+
n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2)
7+
n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1)
8+
return n

0 commit comments

Comments
 (0)