1-liner in Python


  • 0
    O
    class Solution(object):
        def reverseBits(self, n):
            return sum((n>>i&1)<<(31-i) for i in range(32))
    

    Longer but clearer version:

    class Solution(object):
        def reverseBits(self, n):
            ret = 0
            for i in range(32):
                b = (n>>i) & 1
                ret += b<<(31-i)
            return ret
    

Log in to reply
 

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