Skip to content
Related Articles

Related Articles

Reverse actual bits of the given number

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 03 Aug, 2022
View Discussion
Improve Article
Save Article

Given a non-negative integer n. The problem is to reverse the bits of n and print the number obtained after reversing the bits. Note that the actual binary representation of the number is being considered for reversing the bits, no leading 0’s are being considered.
Examples : 
 

Input : 11
Output : 13
(11)10 = (1011)2.
After reversing the bits we get:
(1101)2 = (13)10.

Input : 10
Output : 5
(10)10 = (1010)2.
After reversing the bits we get:
(0101)2 = (101)2
        = (5)10.

 

Recommended Practice

In this approach, one by one bit in the binary representation of n is being obtained with the help of bitwise right shift operation and they are being accumulated in rev with the help of bitwise left shift operation. 
Algorithm: 
 

 

C++




// C++ implementation to reverse bits of a number
#include <bits/stdc++.h>
 
using namespace std;
 
// function to reverse bits of a number
unsigned int reverseBits(unsigned int n)
{
    unsigned int rev = 0;
 
    // traversing bits of 'n' from the right
    while (n > 0) {
        // bitwise left shift
        // 'rev' by 1
        rev <<= 1;
 
        // if current bit is '1'
        if (n & 1 == 1)
            rev ^= 1;
 
        // bitwise right shift
        // 'n' by 1
        n >>= 1;
    }
 
    // required number
    return rev;
}
 
// Driver program to test above
int main()
{
    unsigned int n = 11;
    cout << reverseBits(n);
    return 0;
}

C




// C implementation to reverse bits of a number
#include <stdio.h>
 
// function to reverse bits of a number
unsigned int reverseBits(unsigned int n)
{
    unsigned int rev = 0;
 
    // traversing bits of 'n' from the right
    while (n > 0) {
        // bitwise left shift 'rev' by 1
        rev <<= 1;
 
        // if current bit is '1'
        if (n & 1 == 1)
            rev ^= 1;
 
        // bitwise right shift 'n' by 1
        n >>= 1;
    }
    // required number
    return rev;
}
 
// Driver program to test above
int main()
{
    unsigned int n = 11;
    printf("%u",reverseBits(n));
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta (kriSania804)

Java




// Java implementation to
// reverse bits of a number
class GFG
{
    // function to reverse bits of a number
    public static int reverseBits(int n)
    {
        int rev = 0;
 
        // traversing bits of 'n'
        // from the right
        while (n > 0)
        {
            // bitwise left shift
            // 'rev' by 1
            rev <<= 1;
 
            // if current bit is '1'
            if ((int)(n & 1) == 1)
                rev ^= 1;
 
            // bitwise right shift
            //'n' by 1
            n >>= 1;
        }
        // required number
        return rev;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 11;
        System.out.println(reverseBits(n));
    }
}
 
// This code is contributed
// by prerna saini.

Python3




# Python 3 implementation to
# reverse bits of a number
 
 
# function to reverse
# bits of a number
def reverseBits(n) :
     
    rev = 0
     
    # traversing bits of 'n' from the right
    while (n > 0) :
         
        # bitwise left shift 'rev' by 1
        rev = rev << 1
         
        # if current bit is '1'
        if (n & 1 == 1) :
            rev = rev ^ 1
         
        # bitwise right shift 'n' by 1
        n = n >> 1
         
     
    # required number
    return rev
     
# Driver code
n = 11
print(reverseBits(n))
 
 
# This code is contributed
# by Nikita Tiwari.

C#




// C# implementation to
// reverse bits of a number
using System;
class GFG
{
    // function to reverse bits of a number
    public static int reverseBits(int n)
    {
        int rev = 0;
 
        // traversing bits of 'n'
        // from the right
        while (n > 0)
        {
            // bitwise left shift
            // 'rev' by 1
            rev <<= 1;
 
            // if current bit is '1'
            if ((int)(n & 1) == 1)
                rev ^= 1;
 
            // bitwise right shift
            //'n' by 1
            n >>= 1;
        }
        // required number
        return rev;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 11;
        Console.WriteLine(reverseBits(n));
    }
}
 
// This code is contributed
// by vt_m.

PHP




<?php
// PHP implementation to reverse
// bits of a number
 
// function to reverse
// bits of a number
function reverseBits($n)
{
    $rev = 0;
     
    // traversing bits of 'n'
    // from the right
    while ($n > 0)
    {
        // bitwise left shift
        // 'rev' by 1
        $rev <<= 1;
         
        // if current bit is '1'
        if ($n & 1 == 1)
            $rev ^= 1;
         
        // bitwise right shift
        // 'n' by 1
        $n >>= 1;
             
    }
     
    // required number
    return $rev;
}
 
// Driver code
$n = 11;
echo reverseBits($n);
 
// This code is contributed by mits
?>

Javascript




<script>
 
// JavaScript program  to
// reverse bits of a number
 
    // function to reverse bits of a number
    function reverseBits(n)
    {
        let rev = 0;
   
        // traversing bits of 'n'
        // from the right
        while (n > 0)
        {
            // bitwise left shift
            // 'rev' by 1
            rev <<= 1;
   
            // if current bit is '1'
            if ((n & 1) == 1)
                rev ^= 1;
   
            // bitwise right shift
            //'n' by 1
            n >>= 1;
        }
        // required number
        return rev;
    }
 
// Driver code
 
        let n = 11;
        document.write(reverseBits(n));
 
</script>

Output

13

Time Complexity: O(num), where num is the number of bits in the binary representation of n.

Space Complexity: O(1)
 

How about considering even the leading zero bits for reversal?

Another twist to this problem is to reverse all 4 bytes of an integer value. For e.g. if number is 11 (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1) then its reverse will be -805306368 (1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0).

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.List;
import java.util.ArrayList;
 
class ReverseBits190 {
    public static void main (String[] args) {
          int num = 11;
        showBits(num); // just to show full bit sequence
          int ret = reverseBits(num);
        System.out.println("\nreverse of number " + num + " is=" + ret);
          showBits(ret);
 
          System.out.println("\n");
        num = -10;
        showBits(num); // just to show full bit sequence
          ret = reverseBits(num);
        System.out.println("\nreverse of number " + num + " is=" + ret);
          showBits(ret);
    }
 
      static int reverseBits(int n) {
        int newN = 0;
        for(int i = 0; i < Integer.SIZE; i++) {
            newN = newN << 1;
            if((n & 1) > 0) {
                newN = newN ^ 1;
            }
            n = n >> 1;
        }
        return newN;
    }
 
  // helper method to show actual bits
      static void showBits(int n) {
        List<Integer> l = new ArrayList<>();
        for(int i = 0; i< Integer.SIZE; i++) {
             
            if((n & 1) > 0) l.add(1);
            else l.add(0);
             
            n = n >> 1;
        }
        for(int i = l.size()-1; i >= 0; i--) {
            System.out.print(l.get(i) + ",");
        }
    }
}

Output

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,
reverse of number 11 is=-805306368
1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,
reverse of number -10 is=1879048191
0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,

Contributed by Nikhil.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!