Reverse actual bits of the given number Last Updated : 25 Mar, 2025 Comments Improve Suggest changes Like Article Like Report 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 : 11Output : 13Explanation: (11)10 = (1011)2.After reversing the bits we get: (1101)2 = (13)10.Input : 10Output : 5Explanation : (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++ // 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 // 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 # 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# // 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 // 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)); Output13Time Complexity: O(1)Space Complexity: O(1) Comment More infoAdvertise with us Next Article Reverse actual bits of the given number A ayushjauhari14 Follow Improve Article Tags : Bit Magic DSA Reverse Practice Tags : Bit MagicReverse Similar Reads Set all the bits in given range of a number Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex 5 min read Set the K-th bit of a given number Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 1 4 min read Invert actual bits of a number Given a non-negative integer n. The problem is to invert the bits of n and print the number obtained after inverting the bits. Note that the actual binary representation of the number is being considered for inverting the bits, no leading 0âs are being considered. Examples: Input : 11Output : 4(11)1 13 min read Binary representation of a given number Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000 6 min read Write an Efficient C Program to Reverse Bits of a Number Given an unsigned integer, reverse all bits of it and return the number with reversed bits. Input : n = 1Output : 2147483648 Explanation : On a machine with size of unsigned bit as 32. Reverse of 0....001 is 100....0. Input : n = 2147483648Output : 1 Recommended PracticeReverse BitsTry It!Method1 - 6 min read Like