|
| 1 | +//! This module provides a function to reverse the bits of a 32-bit unsigned integer. |
| 2 | +//! |
| 3 | +//! The algorithm works by iterating through each of the 32 bits from least |
| 4 | +//! significant to most significant, extracting each bit and placing it in the |
| 5 | +//! reverse position. |
| 6 | +//! |
| 7 | +//! # Algorithm |
| 8 | +//! |
| 9 | +//! For each of the 32 bits: |
| 10 | +//! 1. Shift the result left by 1 to make room for the next bit |
| 11 | +//! 2. Extract the least significant bit of the input using bitwise AND with 1 |
| 12 | +//! 3. OR that bit into the result |
| 13 | +//! 4. Shift the input right by 1 to process the next bit |
| 14 | +//! |
| 15 | +//! # Time Complexity |
| 16 | +//! |
| 17 | +//! O(1) - Always processes exactly 32 bits |
| 18 | +//! |
| 19 | +//! # Space Complexity |
| 20 | +//! |
| 21 | +//! O(1) - Uses a constant amount of extra space |
| 22 | +//! |
| 23 | +//! # Example |
| 24 | +//! |
| 25 | +//! ``` |
| 26 | +//! use the_algorithms_rust::bit_manipulation::reverse_bits; |
| 27 | +//! |
| 28 | +//! let n = 43261596; // Binary: 00000010100101000001111010011100 |
| 29 | +//! let reversed = reverse_bits(n); |
| 30 | +//! assert_eq!(reversed, 964176192); // Binary: 00111001011110000010100101000000 |
| 31 | +//! ``` |
| 32 | +
|
| 33 | +/// Reverses the bits of a 32-bit unsigned integer. |
| 34 | +/// |
| 35 | +/// # Arguments |
| 36 | +/// |
| 37 | +/// * `n` - A 32-bit unsigned integer whose bits are to be reversed |
| 38 | +/// |
| 39 | +/// # Returns |
| 40 | +/// |
| 41 | +/// A 32-bit unsigned integer with bits in reverse order |
| 42 | +/// |
| 43 | +/// # Examples |
| 44 | +/// |
| 45 | +/// ``` |
| 46 | +/// use the_algorithms_rust::bit_manipulation::reverse_bits; |
| 47 | +/// |
| 48 | +/// let n = 43261596; // 00000010100101000001111010011100 in binary |
| 49 | +/// let result = reverse_bits(n); |
| 50 | +/// assert_eq!(result, 964176192); // 00111001011110000010100101000000 in binary |
| 51 | +/// ``` |
| 52 | +/// |
| 53 | +/// ``` |
| 54 | +/// use the_algorithms_rust::bit_manipulation::reverse_bits; |
| 55 | +/// |
| 56 | +/// let n = 1; // 00000000000000000000000000000001 in binary |
| 57 | +/// let result = reverse_bits(n); |
| 58 | +/// assert_eq!(result, 2147483648); // 10000000000000000000000000000000 in binary |
| 59 | +/// ``` |
| 60 | +pub fn reverse_bits(n: u32) -> u32 { |
| 61 | + let mut result: u32 = 0; |
| 62 | + let mut num = n; |
| 63 | + |
| 64 | + // Process all 32 bits |
| 65 | + for _ in 0..32 { |
| 66 | + // Shift result left to make room for next bit |
| 67 | + result <<= 1; |
| 68 | + |
| 69 | + // Extract the least significant bit of num and add it to result |
| 70 | + result |= num & 1; |
| 71 | + |
| 72 | + // Shift num right to process the next bit |
| 73 | + num >>= 1; |
| 74 | + } |
| 75 | + |
| 76 | + result |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_reverse_bits_basic() { |
| 85 | + // Test case 1: 43261596 (00000010100101000001111010011100) |
| 86 | + // Expected: 964176192 (00111001011110000010100101000000) |
| 87 | + assert_eq!(reverse_bits(43261596), 964176192); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_reverse_bits_one() { |
| 92 | + // Test case 2: 1 (00000000000000000000000000000001) |
| 93 | + // Expected: 2147483648 (10000000000000000000000000000000) |
| 94 | + assert_eq!(reverse_bits(1), 2147483648); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_reverse_bits_all_ones() { |
| 99 | + // Test case 3: 4294967293 (11111111111111111111111111111101) |
| 100 | + // Expected: 3221225471 (10111111111111111111111111111111) |
| 101 | + assert_eq!(reverse_bits(4294967293), 3221225471); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_reverse_bits_zero() { |
| 106 | + // Test case 4: 0 (00000000000000000000000000000000) |
| 107 | + // Expected: 0 (00000000000000000000000000000000) |
| 108 | + assert_eq!(reverse_bits(0), 0); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_reverse_bits_max() { |
| 113 | + // Test case 5: u32::MAX (11111111111111111111111111111111) |
| 114 | + // Expected: u32::MAX (11111111111111111111111111111111) |
| 115 | + assert_eq!(reverse_bits(u32::MAX), u32::MAX); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_reverse_bits_alternating() { |
| 120 | + // Test case 6: 2863311530 (10101010101010101010101010101010) |
| 121 | + // Expected: 1431655765 (01010101010101010101010101010101) |
| 122 | + assert_eq!(reverse_bits(2863311530), 1431655765); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_reverse_bits_symmetric() { |
| 127 | + // Test case 7: reversing twice should give original number |
| 128 | + let n = 12345678; |
| 129 | + assert_eq!(reverse_bits(reverse_bits(n)), n); |
| 130 | + } |
| 131 | +} |
0 commit comments