Learner Level
Implement the reverseBitsSimple method that returns the integer formed after reversing the bits.
The input is a positive integer n.
Your task is to reverse the bits in its binary representation and return the decimal value of the reversed binary number. Leading zeros after reversal do not affect the decimal result.
For example, 13 is binary 1101. Reversing it gives 1011, which is decimal 11.
Build the reversed number one bit at a time.
Repeatedly take the last bit of n using n % 2. Before adding that bit to the answer, shift the answer left by one position. Then remove the last bit from n by dividing it by two.
When all bits are processed, the answer contains the reversed binary value.
Pseudocode:
function reverseBitsSimple(n):
result = 0
while n > 0:
bit = n % 2
result = result * 2 + bit
n = n / 2
return result