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

**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.

`/* Function to reverse bits of num */` `unsigned ` `int` `reverseBits(unsigned ` `int` `num) ` `{ ` ` ` `unsigned ` `int` `NO_OF_BITS = ` `sizeof` `(num) * 8; ` ` ` `unsigned ` `int` `reverse_num = 0, i, temp; ` ` ` ` ` `for` `(i = 0; i < NO_OF_BITS; i++) ` ` ` `{ ` ` ` `temp = (num & (1 << i)); ` ` ` `if` `(temp) ` ` ` `reverse_num |= (1 << ((NO_OF_BITS - 1) - i)); ` ` ` `} ` ` ` ` ` `return` `reverse_num; ` `} ` ` ` `/* Driver function to test above function */` `int` `main() ` `{ ` ` ` `unsigned ` `int` `x = 2; ` ` ` `printf` `(` `"%u"` `, reverseBits(x)); ` ` ` `getchar` `(); ` `} ` |

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

`unsigned ` `int` `reverseBits(unsigned ` `int` `num) ` `{ ` ` ` `unsigned ` `int` `NO_OF_BITS = ` `sizeof` `(num) * 8; ` ` ` `unsigned ` `int` `reverse_num = 0; ` ` ` `int` `i; ` ` ` `for` `(i = 0; i < NO_OF_BITS; i++) ` ` ` `{ ` ` ` `if` `((num & (1 << i))) ` ` ` `reverse_num |= 1 << ((NO_OF_BITS - 1) - i); ` ` ` `} ` ` ` `return` `reverse_num; ` `} ` |

Time Complexity: O(n)

Space Complexity: O(1)

**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.

`unsigned ` `int` `reverseBits(unsigned ` `int` `num) ` `{ ` ` ` `unsigned ` `int` `count = ` `sizeof` `(num) * 8 - 1; ` ` ` `unsigned ` `int` `reverse_num = num; ` ` ` ` ` `num >>= 1; ` ` ` `while` `(num) ` ` ` `{ ` ` ` `reverse_num <<= 1; ` ` ` `reverse_num |= num & 1; ` ` ` `num >>= 1; ` ` ` `count--; ` ` ` `} ` ` ` `reverse_num <<= count; ` ` ` `return` `reverse_num; ` `} ` ` ` `int` `main() ` `{ ` ` ` `unsigned ` `int` `x = 1; ` ` ` `printf` `(` `"%u"` `, reverseBits(x)); ` ` ` `getchar` `(); ` `} ` |

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. Please refer Reverse bits using lookup table in O(1) time for details.

**Source : **

https://graphics.stanford.edu/~seander/bithacks.html

## Recommended Posts:

- Reverse actual bits of the given number
- Reverse bits using lookup table in O(1) time
- Sum of bit differences among all pairs
- Bitwise Operators in C/C++
- Swap all odd and even bits
- Binary representation of a given number
- Find the element that appears once
- Swap bits in a given number
- Rotate bits of a number
- Count number of bits to be flipped to convert A to B
- Count set bits in an integer
- Little and Big Endian Mystery
- Program to find whether a no is power of two
- Write an Efficient Method to Check if a Number is Multiple of 3
- Smallest power of 2 greater than or equal to n