Given an integer, reverse its bits using binary operators.

The idea is to initialize the result by 0 (all bits 0) and process the given number starting from its least significant bit. If the current bit is 1, then we set the corresponding most significant bit in the result and finally move on to next bit in input number. We repeat this till all its bits are processed.

**C++ implementation –**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> #include <bitset> using namespace std; // Integer size in C #define INT_SIZE 32 // Function to reverse bits of a given integer int reverseBits(int n) { int pos = INT_SIZE - 1; // maintains shift // store reversed bits of n. Initially all bits are set to 0 int reverse = 0; // do till all bits are processed while (pos >= 0 && n) { // if current bit is 1, then set corresponding bit in result if (n & 1) reverse = reverse | (1 << pos); n >>= 1; // drop current bit (divide by 2) pos--; // decrement shift by 1 } return reverse; } // Reverse Bits of a given Integer int main() { int n = -100; cout << n << " in binary is " << bitset<32>(n) << endl; cout << "On reversing bits " << bitset<32>(reverseBits(n)); return 0; } |

`Output:`

-100 in binary is 11111111111111111111111110011100

On reversing bits 00111001111111111111111111111111

Above solution will process all bits in an integer till its last set bit. It can be optimized to consider only set bits in an integer (which will be relatively less). The idea is to find position of the rightmost set bit in the number, and set the corresponding bit in result and finally unset the rightmost set bit. We repeat this till all set bits are processed.

Please refer this post to find position of the rightmost set bit and unset it.

**C++ implementation –**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <iostream> #include <bitset> #include <cmath> using namespace std; // Integer size in C #define INT_SIZE 32 // Function to reverse bits of a given integer int reverseBits(int n) { // store reversed bits of n. Initially all bits are set to 0 int reverse = 0; // do till all set bits are processed while (n) { // find position of the rightmost set bit int pos = log2(n & -n) + 1; // set corresponding bit in result (set leftmost bit at pos) reverse = reverse | (1 << (INT_SIZE - pos)); // unset the rightmost set bit of a number n = n & (n - 1); } return reverse; } // Reverse Bits of a given Integer int main() { int n = -100; cout << n << " in binary is " << bitset<32>(n) << endl; cout << "On reversing bits " << bitset<32>(reverseBits(n)); return 0; } |

`Output:`

-100 in binary is 11111111111111111111111110011100

On reversing bits 00111001111111111111111111111111

**Read More:** Reverse bits of a given integer using lookup table

**Thanks for reading.**

Please use our online compiler to post code in comments. To contribute, get in touch with us.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

With all respect, calculating log2 in a loop doesn’t really sound like an optimization.