Reverse Bits InterviewBit Solution
- illuminati
- Jun 19, 2021
- 1 min read
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++:
Recent Posts
See AllGiven an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Given an array of integers, every element appears thrice except for one which occurs once. Find that element which does not appear thrice.
Given an array of integers A, every element appears twice except for one. Find that single one. NOTE: Your algorithm should have a linear
Comments