Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Reverse the binary representation of a number.

Objec­tive: Write a pro­gram to Reverse the binary rep­re­sen­ta­tion of a number

Exam­ple:

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

Out­put: Dec­i­mal of reversed binary rep­re­sen­ta­tion of a number.

Approach:

  • Ini­tial­ize int res =0
  • Now from a num­ber , 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 num­ber by 1
  • make left shift in res by 1
Reverse Binray representation of a Decimal

Reverse Bin­ray rep­re­sen­ta­tion of a Decimal

Com­plete Code:


Out­put:

Binary rotation of 30 is : 15

 

You may also like...