From 19148a6c8bfa4588660ff4cd8d129ec982ab1d54 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi Date: Mon, 8 Dec 2025 19:44:10 -0800 Subject: [PATCH] Add reverse_bits algorithm to bit_manipulation module --- DIRECTORY.md | 15 +-- src/bit_manipulation/mod.rs | 2 + src/bit_manipulation/reverse_bits.rs | 131 +++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 src/bit_manipulation/reverse_bits.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index cbdc95b5d44..20bebba4c52 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,6 +20,7 @@ * [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs) * [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/highest_set_bit.rs) * [N Bits Gray Code](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/n_bits_gray_code.rs) + * [Reverse Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/reverse_bits.rs) * [Sum Of Two Integers](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/sum_of_two_integers.rs) * Ciphers * [Aes](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/aes.rs) @@ -29,7 +30,7 @@ * [Blake2B](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/blake2b.rs) * [Caesar](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/caesar.rs) * [Chacha](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/chacha.rs) - * [Diffie Hellman](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/diffie_hellman.rs) + * [Diffie-Hellman](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/diffie_hellman.rs) * [Hashing Traits](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/hashing_traits.rs) * [Kerninghan](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/kerninghan.rs) * [Morse Code](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/morse_code.rs) @@ -37,8 +38,8 @@ * [Rail Fence](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rail_fence.rs) * [Rot13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rot13.rs) * [Salsa](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/salsa.rs) - * [Sha256](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha256.rs) - * [Sha3](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha3.rs) + * [SHA-256](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha256.rs) + * [SHA-3](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha3.rs) * [Tea](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/tea.rs) * [Theoretical Rot13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/theoretical_rot13.rs) * [Transposition](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/transposition.rs) @@ -61,9 +62,9 @@ * [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs) * [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs) * [Octal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_hexadecimal.rs) - * [Rgb Cmyk Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs) + * [RGB-CMYK Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs) * Data Structures - * [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs) + * [AVL Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs) * [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs) * [Binary Search Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/binary_search_tree.rs) * [Fenwick Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/fenwick_tree.rs) @@ -78,7 +79,7 @@ * [Count Min Sketch](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/probabilistic/count_min_sketch.rs) * [Queue](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/queue.rs) * [Range Minimum Query](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/range_minimum_query.rs) - * [Rb Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/rb_tree.rs) + * [RB Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/rb_tree.rs) * [Segment Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/segment_tree.rs) * [Segment Tree Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/segment_tree_recursive.rs) * [Stack Using Singly Linked List](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/stack_using_singly_linked_list.rs) @@ -126,7 +127,7 @@ * [Hanoi](https://github.com/TheAlgorithms/Rust/blob/master/src/general/hanoi.rs) * [Huffman Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/general/huffman_encoding.rs) * [Kadane Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/general/kadane_algorithm.rs) - * [Kmeans](https://github.com/TheAlgorithms/Rust/blob/master/src/general/kmeans.rs) + * [K-Means](https://github.com/TheAlgorithms/Rust/blob/master/src/general/kmeans.rs) * [Mex](https://github.com/TheAlgorithms/Rust/blob/master/src/general/mex.rs) * Permutations * [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/general/permutations/heap.rs) diff --git a/src/bit_manipulation/mod.rs b/src/bit_manipulation/mod.rs index 027c4b81817..00a4f77f536 100644 --- a/src/bit_manipulation/mod.rs +++ b/src/bit_manipulation/mod.rs @@ -1,9 +1,11 @@ mod counting_bits; mod highest_set_bit; mod n_bits_gray_code; +mod reverse_bits; mod sum_of_two_integers; pub use counting_bits::count_set_bits; pub use highest_set_bit::find_highest_set_bit; pub use n_bits_gray_code::generate_gray_code; +pub use reverse_bits::reverse_bits; pub use sum_of_two_integers::add_two_integers; diff --git a/src/bit_manipulation/reverse_bits.rs b/src/bit_manipulation/reverse_bits.rs new file mode 100644 index 00000000000..65c08a347f4 --- /dev/null +++ b/src/bit_manipulation/reverse_bits.rs @@ -0,0 +1,131 @@ +//! This module provides a function to reverse the bits of a 32-bit unsigned integer. +//! +//! The algorithm works by iterating through each of the 32 bits from least +//! significant to most significant, extracting each bit and placing it in the +//! reverse position. +//! +//! # Algorithm +//! +//! For each of the 32 bits: +//! 1. Shift the result left by 1 to make room for the next bit +//! 2. Extract the least significant bit of the input using bitwise AND with 1 +//! 3. OR that bit into the result +//! 4. Shift the input right by 1 to process the next bit +//! +//! # Time Complexity +//! +//! O(1) - Always processes exactly 32 bits +//! +//! # Space Complexity +//! +//! O(1) - Uses a constant amount of extra space +//! +//! # Example +//! +//! ``` +//! use the_algorithms_rust::bit_manipulation::reverse_bits; +//! +//! let n = 43261596; // Binary: 00000010100101000001111010011100 +//! let reversed = reverse_bits(n); +//! assert_eq!(reversed, 964176192); // Binary: 00111001011110000010100101000000 +//! ``` + +/// Reverses the bits of a 32-bit unsigned integer. +/// +/// # Arguments +/// +/// * `n` - A 32-bit unsigned integer whose bits are to be reversed +/// +/// # Returns +/// +/// A 32-bit unsigned integer with bits in reverse order +/// +/// # Examples +/// +/// ``` +/// use the_algorithms_rust::bit_manipulation::reverse_bits; +/// +/// let n = 43261596; // 00000010100101000001111010011100 in binary +/// let result = reverse_bits(n); +/// assert_eq!(result, 964176192); // 00111001011110000010100101000000 in binary +/// ``` +/// +/// ``` +/// use the_algorithms_rust::bit_manipulation::reverse_bits; +/// +/// let n = 1; // 00000000000000000000000000000001 in binary +/// let result = reverse_bits(n); +/// assert_eq!(result, 2147483648); // 10000000000000000000000000000000 in binary +/// ``` +pub fn reverse_bits(n: u32) -> u32 { + let mut result: u32 = 0; + let mut num = n; + + // Process all 32 bits + for _ in 0..32 { + // Shift result left to make room for next bit + result <<= 1; + + // Extract the least significant bit of num and add it to result + result |= num & 1; + + // Shift num right to process the next bit + num >>= 1; + } + + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_reverse_bits_basic() { + // Test case 1: 43261596 (00000010100101000001111010011100) + // Expected: 964176192 (00111001011110000010100101000000) + assert_eq!(reverse_bits(43261596), 964176192); + } + + #[test] + fn test_reverse_bits_one() { + // Test case 2: 1 (00000000000000000000000000000001) + // Expected: 2147483648 (10000000000000000000000000000000) + assert_eq!(reverse_bits(1), 2147483648); + } + + #[test] + fn test_reverse_bits_all_ones() { + // Test case 3: 4294967293 (11111111111111111111111111111101) + // Expected: 3221225471 (10111111111111111111111111111111) + assert_eq!(reverse_bits(4294967293), 3221225471); + } + + #[test] + fn test_reverse_bits_zero() { + // Test case 4: 0 (00000000000000000000000000000000) + // Expected: 0 (00000000000000000000000000000000) + assert_eq!(reverse_bits(0), 0); + } + + #[test] + fn test_reverse_bits_max() { + // Test case 5: u32::MAX (11111111111111111111111111111111) + // Expected: u32::MAX (11111111111111111111111111111111) + assert_eq!(reverse_bits(u32::MAX), u32::MAX); + } + + #[test] + fn test_reverse_bits_alternating() { + // Test case 6: 2863311530 (10101010101010101010101010101010) + // Expected: 1431655765 (01010101010101010101010101010101) + assert_eq!(reverse_bits(2863311530), 1431655765); + } + + #[test] + fn test_reverse_bits_symmetric() { + // Test case 7: reversing twice should give original number + let n = 12345678; + assert_eq!(reverse_bits(reverse_bits(n)), n); + } +}