Given an unsigned integer, reverse all bits of it and return the number with reversed bits.

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

Input : n = 2147483648
Output : 1

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Method1 – Simple
Loop through all the bits of an integer. If a bit at ith position is set in the i/p no. then set the bit at (NO_OF_BITS – 1) – i in o/p. Where NO_OF_BITS is number of bits present in the given number.

[pastacode lang=”c” manual=”%2F*%20Function%20to%20reverse%20bits%20of%20num%20*%2F%0Aunsigned%20int%20reverseBits(unsigned%20int%20num)%0A%7B%0A%20%20%20%20unsigned%20int%20%20NO_OF_BITS%20%3D%20sizeof(num)%20*%208%3B%0A%20%20%20%20unsigned%20int%20reverse_num%20%3D%200%2C%20i%2C%20temp%3B%0A%20%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20NO_OF_BITS%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20temp%20%3D%20(num%20%26%20(1%20%3C%3C%20i))%3B%0A%20%20%20%20%20%20%20%20if(temp)%0A%20%20%20%20%20%20%20%20%20%20%20%20reverse_num%20%7C%3D%20(1%20%3C%3C%20((NO_OF_BITS%20-%201)%20-%20i))%3B%0A%20%20%20%20%7D%0A%20%20%0A%20%20%20%20return%20reverse_num%3B%0A%7D%0A%20%0A%2F*%20Driver%20function%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20unsigned%20int%20x%20%3D%202%3B%20%0A%20%20%20%20printf(%22%25u%22%2C%20reverseBits(x))%3B%0A%20%20%20%20getchar()%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Above program can be optimized by removing the use of variable temp. See below the modified code.

[pastacode lang=”c” manual=”unsigned%20int%20reverseBits(unsigned%20int%20num)%0A%7B%0A%20%20%20%20unsigned%20int%20%20NO_OF_BITS%20%3D%20sizeof(num)%20*%208%3B%0A%20%20%20%20unsigned%20int%20reverse_num%20%3D%200%3B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20NO_OF_BITS%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if((num%20%26%20(1%20%3C%3C%20i)))%0A%20%20%20%20%20%20%20%20%20%20%20reverse_num%20%7C%3D%201%20%3C%3C%20((NO_OF_BITS%20-%201)%20-%20i)%3B%20%20%0A%20%20%20%7D%0A%20%20%20%20return%20reverse_num%3B%0A%7D” message=”C ” highlight=”” provider=”manual”/]

Time Complexity: O(log n)
Space Complexity: O(1)

[ad type=”banner”]

Method 2 – Standard

The idea is to keep putting set bits of the num in reverse_num until num becomes zero. After num becomes zero, shift the remaining bits of reverse_num.

Let num is stored using 8 bits and num be 00000110. After the loop you will get reverse_num as 00000011. Now you need to left shift reverse_num 5 more times and you get the exact reverse 01100000.

[pastacode lang=”c” manual=”unsigned%20int%20reverseBits(unsigned%20int%20num)%0A%7B%0A%20%20%20%20unsigned%20int%20count%20%3D%20sizeof(num)%20*%208%20-%201%3B%0A%20%20%20%20unsigned%20int%20reverse_num%20%3D%20num%3B%0A%20%20%20%20%20%0A%20%20%20%20num%20%3E%3E%3D%201%3B%20%0A%20%20%20%20while(num)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20reverse_num%20%3C%3C%3D%201%3B%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20reverse_num%20%7C%3D%20num%20%26%201%3B%0A%20%20%20%20%20%20%20num%20%3E%3E%3D%201%3B%0A%20%20%20%20%20%20%20count–%3B%0A%20%20%20%20%7D%0A%20%20%20%20reverse_num%20%3C%3C%3D%20count%3B%0A%20%20%20%20return%20reverse_num%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20unsigned%20int%20x%20%3D%201%3B%0A%20%20%20%20printf(%22%25u%22%2C%20reverseBits(x))%3B%0A%20%20%20%20getchar()%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Time Complexity: O(log n)
Space Complexity: O(1)

Method 3 – Lookup Table:
We can reverse the bits of a number in O(1) if we know the size of the number. We can implement it using look up table. Go through the below link for details. You will find some more interesting bit related stuff there..

[ad type=”banner”]