15 / 75
E

Reverse Bits

#15
bit-manipulationdivide-and-conquer

Reverse the bits of a 32-bit unsigned integer.

Example:

Input:00000010100101000001111010011100
Output:00111001011110000010100101000000

Common Mistakes:

  • Not processing all 32 bits
  • Confusing with reversing integer value
  • Not using unsigned right shift (>>> in some contexts)

Notes:

Process bit by bit: extract rightmost bit from input, add to leftmost position of result. Use >>> for unsigned shift if needed. Time: O(1) - always 32 bits.

15/75
Reverse Bits