AlgoMaster Logo

Reverse Bits

Ashish

Ashish Pratap Singh

easy

Problem Description

Solve it on LeetCode

Approaches

1. Simple Bit Manipulation

Intuition:

The problem of reversing bits can be solved by taking each bit from the rightmost side of the input number and placing it in the leftmost side of the result number. We need to iterate over all 32 bits since the function requires us to handle a 32-bit unsigned integer.

Steps:

  1. Initialize a result variable to zero to store the reversed bits.
  2. Use a loop to process all 32 bits of the number.
  3. In each iteration, check if the least significant bit of the number is 1.
  4. If it is 1, set the corresponding bit in the result by using left shift on the result and then OR operation.
  5. Right shift the input number to process the next bit.
  6. After processing all 32 bits, the 'result' variable will contain the reversed bits.

Code:

2. Efficient Bit Manipulation

Intuition:

Reversing bits one-by-one is slow because each bit must be individually inspected and moved. A far more efficient approach is to reverse bits in groups using masks and shifts.

The idea is:

  • First swap neighboring bits (groups of 1).
  • Then swap groups of 2 bits.
  • Then 4-bit groups (nibbles).
  • Then 8-bit groups (bytes).
  • Then 16-bit groups (half-words).

By progressively doubling the size of the swapped chunks, we can reverse all 32 bits in only 5 operations, dramatically faster than looping through all bits.

This method exploits the power of:

  • bit masks to isolate specific chunks of bits
  • bit shifts to move those chunks into their reversed positions
  • bitwise OR to recombine the results
How the masks work:

Let’s imagine the 32-bit number as:

Steps:

  1. Use masks to isolate and swap bits at target positions.
  2. Start by swapping adjacent pairs, followed by swapping nibbles (4-bit groups), then octets (8-bit groups), and finally by half-words (16-bit groups).
  3. This approach exploits the property of bit manipulation using masks and shifts to achieve a similar result in fewer operations.

Code: