Reverse Bits InterviewBit Solution

Problem: Reverse Bits


Problem Description

Reverse the bits of a 32-bit unsigned integer A.


Problem Constraints

0 <= A <= 232


Input Format

The first and only argument of input contains an integer A.


Output Format

Return a single unsigned integer denoting the decimal value of reversed bits.



Example Input

Input 1:
 0
Input 2:
 3

Example Output

Output 1:
 0
Output 2:
 3221225472

Example Explanation

Explanation 1:

        00000000000000000000000000000000
=>      00000000000000000000000000000000 

Explanation 2:

        00000000000000000000000000000011     
=>      11000000000000000000000000000000 

Solution Approach:

To solve this problem first find the positions of set bits in the original number then mirror those set bits according to that in your resultant variable.


That is for example if ith bit is a set bit in original number then set the (31-i)th bit of resultant number.





Solution in C++: