Reverse actual bits of the given number
Last Updated : 25 Mar, 2025
Improve
Try it on GfG Practice
Given a non-negative integer n, the task is to reverse the bits in its binary representation and return the resulting decimal number. The reversal should consider only the actual binary digits without any leading zeros.
Examples :
Input : 11
Output : 13
Explanation: (11)10 = (1011)2.
After reversing the bits we get: (1101)2 = (13)10.Input : 10
Output : 5
Explanation : (10)10 = (1010)2.
After reversing the bits we get: (0101)2 = (101)2 = (5)10.
Approach:
The idea is to build the reversed number by examining each bit of the input number from right to left, while simultaneously constructing the result from left to right using left shift and right shift operators.
Step by step approach:
- Traverse each bit of the input number using right shift operations (n >>= 1).
- Build the result by shifting it left (ans <<= 1) before each new bit is processed.
- When a '1' bit is encountered in the input (n & 1 == 1), set the rightmost bit of the result (ans |= 1).
- Continue this process until all bits in the input number have been processed (n > 0).
// C++ program to reverse actual bits of a given number
#include <iostream>
using namespace std;
int reverseBits(unsigned int n) {
int ans = 0;
// traversing bits of 'n' from the right
while (n > 0) {
// bitwise left shift
// 'ans' by 1
ans <<= 1;
// if current bit is '1'
if (n & 1 == 1)
ans |= 1;
// bitwise right shift
// 'n' by 1
n >>= 1;
}
// required number
return ans;
}
int main() {
int n = 11;
cout << reverseBits(n);
return 0;
}
// Java program to reverse actual bits of a given number
class GfG {
static int reverseBits(int n) {
int ans = 0;
// traversing bits of 'n' from the right
while (n > 0) {
// bitwise left shift
// 'ans' by 1
ans <<= 1;
// if current bit is '1'
if ((n & 1) == 1)
ans |= 1;
// bitwise right shift
// 'n' by 1
n >>= 1;
}
// required number
return ans;
}
public static void main(String[] args) {
int n = 11;
System.out.println(reverseBits(n));
}
}
# Python program to reverse actual bits of a given number
def reverseBits(n):
ans = 0
# traversing bits of 'n' from the right
while n > 0:
# bitwise left shift
# 'ans' by 1
ans <<= 1
# if current bit is '1'
if (n & 1) == 1:
ans |= 1
# bitwise right shift
# 'n' by 1
n >>= 1
# required number
return ans
if __name__ == "__main__":
n = 11
print(reverseBits(n))
// C# program to reverse actual bits of a given number
using System;
class GfG {
static uint reverseBits(uint n) {
uint ans = 0;
// traversing bits of 'n' from the right
while (n > 0) {
// bitwise left shift
// 'ans' by 1
ans <<= 1;
// if current bit is '1'
if ((n & 1) == 1)
ans |= 1;
// bitwise right shift
// 'n' by 1
n >>= 1;
}
// required number
return ans;
}
static void Main() {
uint n = 11;
Console.WriteLine(reverseBits(n));
}
}
// JavaScript program to reverse actual bits of a given number
function reverseBits(n) {
let ans = 0;
// traversing bits of 'n' from the right
while (n > 0) {
// bitwise left shift
// 'ans' by 1
ans <<= 1;
// if current bit is '1'
if ((n & 1) === 1)
ans |= 1;
// bitwise right shift
// 'n' by 1
n >>= 1;
}
// required number
return ans;
}
let n = 11;
console.log(reverseBits(n));
Output
13
Time Complexity: O(1)
Space Complexity: O(1)