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.
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.
In this approach, one by one bits in binary representation of n are 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.
Time Complexity: O(num), where num is the number of bits in the binary representation of n.
- Invert actual bits of a number
- Write an Efficient C Program to Reverse Bits of a Number
- Check if actual binary representation of a number is palindrome
- Reverse bits using lookup table in O(1) time
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Check if a number has same number of set and unset bits
- Same Number Of Set Bits As N
- Set all even bits of a number
- Set all odd bits of a number
- Swap bits in a given number
- Set all the bits in given range of a number
- For every set bit of a number toggle bits of other
- Rotate bits of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar