Python AC, with shift and mask


  • 0
    M

    class Solution:
    # @param n, an integer
    # @return an integer
    def reverseBits(self, n):
    ans = 0
    for i in range(0,32):
    #get information
    x = 1 & n
    # shift the input num by 1
    n >>= 1
    # set the leftmost bit in input num to 0 becuase python shift is arithmetic
    n &= 0x7fffffff
    #move the x to correct location
    x <<= (31- i)
    # set the bit in the answer
    ans |= x
    return ans


  • 0
    Z

    Sorry, your code format is very hard understanding...

    Could you please fix it...

    I solved it:

    class Solution: 
        # @param n, an integer
        # @return an integer
        def reverseBits(self, n):
            mask = 0
            for i in range(31):
                mask |= (n & 1)
                mask <<= 1
                n >>= 1
            return n | mask

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.