My solution with bit manipulation in python

  • 1

    My solution with bit manipulation:

    class Solution(object):

    def reverseBits(self, n):
        :type n: int
        :rtype: int
        ret = 0
        for i in range(32):
            if n & (1 << i):  # update ret (31-i)th  bit when the ith bit at n is '1'
                ret |= 1 << (31 - i)
        return ret

Log in to reply

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