Question: https://leetcode.com/problems/reverse-bits/
Question Name: Reverse Bits
Exactly same challege from Elements of Programming Interviews.
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution: # @param n, an integer # @return an integer def reverseBits(self, n): transfer_table = {0b0000 : 0b0000, 0b0001 : 0b1000, 0b0010 : 0b0100, 0b0011 : 0b1100, 0b0100 : 0b0010, 0b0101 : 0b1010, 0b0110 : 0b0110, 0b0111 : 0b1110, 0b1000 : 0b0001, 0b1001 : 0b1001, 0b1010 : 0b0101, 0b1011 : 0b1101, 0b1100 : 0b0011, 0b1101 : 0b1011, 0b1110 : 0b0111, 0b1111 : 0b1111} result = 0 for shift in xrange(0, 32, 4): # Process 4 bits in each round result = (result << 4) | transfer_table[(n >> shift) & 0x0F] return result |