Reverse Bits
Problem Statement
Section titled “Problem Statement”Reverse the bits of a given 32-bit unsigned integer.
You need to solve this problem efficiently.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
| Example 1 | Result 1 | Explanation for example 1 |
| Example 2 | Result 2 | Explanation for example 2 |
Constraints
Section titled “Constraints”- Constraint 1 for Reverse Bits
- Constraint 2
- Constraint 3
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Iterate | O(n) | O(1) | When applicable |
| Bit Manipulation | O(n) | O(1) | When applicable |
| Byte Reversal | O(n) | O(1) | When applicable |
Approach 1: Iterate
Section titled “Approach 1: Iterate”This approach provides an efficient solution for reverse bits.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”def reverse_bits_iterate(n: int) -> int: """Reverse bits of a 32-bit unsigned integer by iteration.""" result = 0 for i in range(32): result = (result << 1) | (n & 1) n >>= 1 return result
# Test casesprint(reverse_bits_iterate(43261596)) # 964176192#include <cstdint>using namespace std;
uint32_t reverseBitsIterate(uint32_t n) { uint32_t result = 0; for (int i = 0; i < 32; i++) { result = (result << 1) | (n & 1); n >>= 1; } return result;}
int main() { cout << reverseBitsIterate(43261596) << endl; // 964176192 return 0;}public class ReverseBitsIterate { public static int reverseBitsIterate(int n) { int result = 0; for (int i = 0; i < 32; i++) { result = (result << 1) | (n & 1); n >>>= 1; } return result; }
public static void main(String[] args) { System.out.println(reverseBitsIterate(43261596)); }}function reverseBitsIterate(n) { let result = 0; for (let i = 0; i < 32; i++) { result = (result << 1) | (n & 1); n >>>= 1; } return result >>> 0;}
console.log(reverseBitsIterate(43261596)); // 964176192fn reverse_bits_iterate(mut n: u32) -> u32 { let mut result = 0; for _ in 0..32 { result = (result << 1) | (n & 1); n >>= 1; } result}
fn main() { println!("{}", reverse_bits_iterate(43261596)); // 964176192}package main
import "fmt"
func reverseBitsIterate(num uint32) uint32 { result := uint32(0) for i := 0; i < 32; i++ { result = (result << 1) | (num & 1) num >>= 1 } return result}
func main() { fmt.Println(reverseBitsIterate(43261596)) // 964176192}Approach 2: Bit Manipulation
Section titled “Approach 2: Bit Manipulation”This approach provides an efficient solution for reverse bits.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”def reverse_bits_bit_manipulation(n: int) -> int: """ Bit manipulation approach - reverse bits one by one using bit operations. Time: O(1) - exactly 32 iterations for a 32-bit integer Space: O(1) - constant extra space """ result = 0 for i in range(32): # Extract the rightmost bit of n bit = n & 1 # Shift result left and add the bit result = (result << 1) | bit # Shift n right for next iteration n >>= 1 return result
print(reverse_bits_bit_manipulation(0b00000010100101000001111010011100)) # 964176192print(reverse_bits_bit_manipulation(0b11111111111111111111111111111101)) # 3221225469# C++ Solution# Problem: reverse-bits# Approach: bit_manipulation
# Implementation placeholder for reverse_bits_bit_manipulation# This file is auto-generated and should contain the solution code.
def reverse_bits_bit_manipulation(): pass
if __name__ == "__main__": pass# Java Solution# Problem: reverse-bits# Approach: bit_manipulation
# Implementation placeholder for reverse_bits_bit_manipulation# This file is auto-generated and should contain the solution code.
def reverse_bits_bit_manipulation(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: reverse-bits# Approach: bit_manipulation
# Implementation placeholder for reverse_bits_bit_manipulation# This file is auto-generated and should contain the solution code.
def reverse_bits_bit_manipulation(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: reverse-bits# Approach: bit_manipulation
# Implementation placeholder for reverse_bits_bit_manipulation# This file is auto-generated and should contain the solution code.
def reverse_bits_bit_manipulation(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: reverse-bits# Approach: bit_manipulation
# Implementation placeholder for reverse_bits_bit_manipulation# This file is auto-generated and should contain the solution code.
def reverse_bits_bit_manipulation(): pass
if __name__ == "__main__": passApproach 3: Byte Reversal
Section titled “Approach 3: Byte Reversal”This approach provides an efficient solution for reverse bits.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: reverse-bits# Approach: byte_reversal
# Implementation placeholder for reverse_bits_byte_reversal# This file is auto-generated and should contain the solution code.
def reverse_bits_byte_reversal(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: reverse-bits# Approach: byte_reversal
# Implementation placeholder for reverse_bits_byte_reversal# This file is auto-generated and should contain the solution code.
def reverse_bits_byte_reversal(): pass
if __name__ == "__main__": pass# Java Solution# Problem: reverse-bits# Approach: byte_reversal
# Implementation placeholder for reverse_bits_byte_reversal# This file is auto-generated and should contain the solution code.
def reverse_bits_byte_reversal(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: reverse-bits# Approach: byte_reversal
# Implementation placeholder for reverse_bits_byte_reversal# This file is auto-generated and should contain the solution code.
def reverse_bits_byte_reversal(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: reverse-bits# Approach: byte_reversal
# Implementation placeholder for reverse_bits_byte_reversal# This file is auto-generated and should contain the solution code.
def reverse_bits_byte_reversal(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: reverse-bits# Approach: byte_reversal
# Implementation placeholder for reverse_bits_byte_reversal# This file is auto-generated and should contain the solution code.
def reverse_bits_byte_reversal(): pass
if __name__ == "__main__": pass