w3resource logo
Python Exercises

Python Challenges: Reverse the bits of an integer(32 bits unsigned)

Python Challenges - 1: Exercise-19 with Solution

Write a Python program to reverse the bits of an integer (32 bits unsigned).

Explanation:

Python: Reverse the bits of an integer(32 bits unsigned)

Sample Solution:-

Python Code:

def reverse_Bits(n):
        result = 0
        for i in range(32):
            result <<= 1
            result |= n & 1
            n >>= 1
        return result
            
print(reverse_Bits(1234))

Sample Output:

1260388352

Flowchart:

Python Flowchart: Reverse the digits of an integer(32 bits unsigned)

Python Code Editor:

def reverse_Bits(n):
        result = 0
        for i in range(32):
            result <<= 1
            result |= n & 1
            n >>= 1
        return result
            
print(reverse_Bits(1234))

Improve this sample solution and post your code through Disqus