Skip to content
Related Articles
Open in App
Not now

Related Articles

Reverse bits of a positive integer number in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 26 Dec, 2022
Improve Article
Save Article
Like Article

Given an positive integer and size of bits, reverse all bits of it and return the number with reversed bits.Examples:

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is

Input : n = 2147483648, bitSize=32
Output : 1  

We can solve this problem quickly in Python. Approach is very simple,

  1. Convert integer number into it’s binary representation using bin(num) function.
  2. bin() function appends 0b as a prefix in binary representation of number, skip first two characters of binary representation and reverse remaining part of string.
  3. As we know in memory any binary representation of a number is filled with leading zeros after last set bit from left that means we need to append bitSize – len(reversedBits) number of zeros after reversing remaining string.
  4. Now convert binary representation into integer number using int(string,base) method.

How int() method works ?

int(string,base) method takes a string and base to identify that string is referring to what number system ( binary=2, hexadecimal=16, octal=8 etc. ) and converts string into decimal number system accordingly. For example ; int(‘1010’,2) = 10.


# Function to reverse bits of positive 
# integer number
def reverseBits(num,bitSize):
    # Convert number into binary representation
    # output will be like bin(10) = '0b10101'
    binary = bin(num)
    # Skip first two characters of binary
    # representation string and reverse
    # remaining string and then append zeros
    # after it. binary[-1:1:-1]  --> start
    # from last character and reverse it until
    # second last character from left
    reverse = binary[-1:1:-1]
    reverse = reverse + (bitSize - len(reverse))*'0'
    # converts reversed binary string into integer
    print (int(reverse,2))  
# Driver program
if __name__ == '__main__':
    num = 1
    bitSize = 32
    reverseBits(num, bitSize)



Another way to revert bits without converting to string:

This is based on the concept that if the number (say N) is reversed for X bit then the reversed number will have the value same as:
the maximum possible number of X bits – N
= 2X – 1 – N

Follow the steps to implement this idea:

  • Find the highest number that can be formed by the given number.
  • Subtract the given number from that.
  • Return the numbers.

Below is the implementation of the given approach.


# Python code to implement the approach
# Function to find the reverse of the number
def reverse_bits(number, bit_size):   
    # for example, if bitSize is 32   
    # then after 1 << bitSize we will get
    # a 1 in 33-th bit position   
    # bin(1 << bitSize) looks like
    # '0b100000000000000000000000000000000'   
    # so to get all 1 in each 32 bit positions,
    # we need to subtract 1 from (1 << bitSize)
    max_value = (1 << bit_size) - 1
    # it is the maximum value for unsigned int32
    # then just subtract your number from the maximum
    return max_value - number
if __name__ == "__main__":
    # for example we can get the number 56
    num = 156
    # choose a binary size which we want to reverse
    size = 32
    print(reverse_bits(num, size))



My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!