This post is completed by 2 users

  • 1
Add to List
Medium

33. 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

Approach:

  • Initialize int res =0
  • Now from the given 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
Binary rotation of 30 is : 15