Reverse bits of a positive integer number in Python
Last Updated :
10 Apr, 2023
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
100....0.
Input : n = 2147483648, bitSize=32
Output : 1
We can solve this problem quickly in Python. Approach is very simple,
- Convert integer number into it’s binary representation using bin(num) function.
- 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.
- 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.
- 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.
Python3
def reverseBits(num,bitSize):
binary = bin (num)
reverse = binary[ - 1 : 1 : - 1 ]
reverse = reverse + (bitSize - len (reverse)) * '0'
print ( int (reverse, 2 ))
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.
Python3
def reverse_bits(number, bit_size):
max_value = ( 1 << bit_size) - 1
return max_value - number
if __name__ = = "__main__" :
num = 156
size = 32
print (reverse_bits(num, size))
|
Approach#3: Using bit manipulation
This approach reverses the bits of a given positive integer number n with the given bit size bitSize. It iterates through all the bits of n using a for loop and checks if the i-th bit is set to 1 by performing a bitwise AND operation between n and a left-shifted 1 by i. If it’s set, it sets the corresponding bit in the result variable using a bitwise OR operation between result and a left-shifted 1 by bitSize – 1 – i. Finally, it returns the result.
Algorithm
1. Initialize result to 0.
2. Iterate through each bit position from 0 to 31:
a. If the bit in the input number is set, set the corresponding bit in the result to 1 shifted by 31 minus the bit position.
3. Return the result.
Python3
def reverse_bits(n, bitSize):
result = 0
for i in range (bitSize):
if n & ( 1 << i):
result | = 1 << (bitSize - 1 - i)
return result
n = 2147483648
bitSize = 32
print (reverse_bits(n, bitSize))
|
Time Complexity: O(1), since we always iterate through 32 bits.
Space Complexity: O(1), since we only use a constant amount of memory for variables.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...