w3resource

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:

Contribute your code and comments through Disqus.

Previous: Write a Python program to reverse the digits of an integer.
Next: Write a Python program to check a sequence of numbers is an arithmetic progression or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/challenges/1/python-challenges-1-exercise-19.php