Reverse the binary representation of a number.

Objective: Write a program to Reverse the binary representation of a number

Example:

Input : 30
Output : 15

Explanation:

binary representation of 30 is : 11110
reverse of binary representation : 01111
decimal of reversed binary representation is : 15

Input: A Number

Output: Decimal of reversed binary representation of a number.

Approach:

  • Initialize int res =0
  • Now from a number , take one bit at a time
  • take AND of that bit with 1 and then OR with res and store it in res
  • make right shift in number by 1
  • make left shift in res by 1
Reverse Binray representation of a Decimal

Reverse Binray representation of a Decimal

Complete Code:

Output:

Binary rotation of 30 is : 15

 

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: