Skip to main content
Back to problems
#190
Easy Algorithms

Reverse bits

Divide and Conquer Bit Manipulation
67.8% acceptance
Jan 12, 2026
5871
1675
Reverse bits of a given 32 bits signed integer.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn reverse_bits(n: i32) -> i32 {
    let mut n = n as u32;
    let mut result = 0u32;
    
    for _ in 0..32 {
      result = (result << 1) | (n & 1);
      n >>= 1;
    }
    
    result as i32
  }
}