Practice Problem

Reverse Bits

Difficulty: Easy

Reverse all 32 bits of a given unsigned integer and return the result as an unsigned integer.

Reverse Bits

Reverse the bits of a given 32-bit unsigned integer.

Note: In some languages, such as JavaScript, there is no unsigned integer type. The input will be given as a signed integer, but the internal binary representation is the same whether signed or unsigned. In JavaScript, use the unsigned right shift operator >>> to treat the number as unsigned.

Examples

Example 1:

Input: n = 43261596
Output: 964176192
Explanation:
  Input binary:  00000010100101000001111010011100
  Output binary: 00111001011110000010100101000000
  Which is 964176192 in decimal.

Example 2:

Input: n = 4294967293
Output: 3221225471
Explanation:
  Input binary:  11111111111111111111111111111101
  Output binary: 10111111111111111111111111111111
  Which is 3221225471 in decimal.

Example 3:

Input: n = 0
Output: 0
Explanation: All bits are 0, so the reversed result is also 0.

Constraints

  • The input must be a 32-bit unsigned integer (0 <= n <= 2^32 - 1).

Expected Complexity

  • Time: O(1). always processes exactly 32 bits
  • Space: O(1)
EASY
Bit Manipulation
Algorithms
Beginner
Free

0 views

Solution

Hints

Hint 1
Hint 2
Premium
Hint 3
Premium
Hint 4
Premium