Skip to content

Commit 6bed9d5

Browse files
Add Reverse Bits algorithm to Bit Manipulation module (#963)
1 parent e753993 commit 6bed9d5

File tree

3 files changed

+141
-7
lines changed

3 files changed

+141
-7
lines changed

DIRECTORY.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs)
2121
* [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/highest_set_bit.rs)
2222
* [N Bits Gray Code](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/n_bits_gray_code.rs)
23+
* [Reverse Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/reverse_bits.rs)
2324
* [Sum Of Two Integers](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/sum_of_two_integers.rs)
2425
* Ciphers
2526
* [Aes](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/aes.rs)
@@ -29,16 +30,16 @@
2930
* [Blake2B](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/blake2b.rs)
3031
* [Caesar](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/caesar.rs)
3132
* [Chacha](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/chacha.rs)
32-
* [Diffie Hellman](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/diffie_hellman.rs)
33+
* [Diffie-Hellman](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/diffie_hellman.rs)
3334
* [Hashing Traits](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/hashing_traits.rs)
3435
* [Kerninghan](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/kerninghan.rs)
3536
* [Morse Code](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/morse_code.rs)
3637
* [Polybius](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/polybius.rs)
3738
* [Rail Fence](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rail_fence.rs)
3839
* [Rot13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rot13.rs)
3940
* [Salsa](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/salsa.rs)
40-
* [Sha256](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha256.rs)
41-
* [Sha3](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha3.rs)
41+
* [SHA-256](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha256.rs)
42+
* [SHA-3](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha3.rs)
4243
* [Tea](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/tea.rs)
4344
* [Theoretical Rot13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/theoretical_rot13.rs)
4445
* [Transposition](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/transposition.rs)
@@ -61,9 +62,9 @@
6162
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
6263
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
6364
* [Octal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_hexadecimal.rs)
64-
* [Rgb Cmyk Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs)
65+
* [RGB-CMYK Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs)
6566
* Data Structures
66-
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
67+
* [AVL Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
6768
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)
6869
* [Binary Search Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/binary_search_tree.rs)
6970
* [Fenwick Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/fenwick_tree.rs)
@@ -78,7 +79,7 @@
7879
* [Count Min Sketch](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/probabilistic/count_min_sketch.rs)
7980
* [Queue](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/queue.rs)
8081
* [Range Minimum Query](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/range_minimum_query.rs)
81-
* [Rb Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/rb_tree.rs)
82+
* [RB Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/rb_tree.rs)
8283
* [Segment Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/segment_tree.rs)
8384
* [Segment Tree Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/segment_tree_recursive.rs)
8485
* [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 @@
126127
* [Hanoi](https://github.com/TheAlgorithms/Rust/blob/master/src/general/hanoi.rs)
127128
* [Huffman Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/general/huffman_encoding.rs)
128129
* [Kadane Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/general/kadane_algorithm.rs)
129-
* [Kmeans](https://github.com/TheAlgorithms/Rust/blob/master/src/general/kmeans.rs)
130+
* [K-Means](https://github.com/TheAlgorithms/Rust/blob/master/src/general/kmeans.rs)
130131
* [Mex](https://github.com/TheAlgorithms/Rust/blob/master/src/general/mex.rs)
131132
* Permutations
132133
* [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/general/permutations/heap.rs)

src/bit_manipulation/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod counting_bits;
22
mod highest_set_bit;
33
mod n_bits_gray_code;
4+
mod reverse_bits;
45
mod sum_of_two_integers;
56

67
pub use counting_bits::count_set_bits;
78
pub use highest_set_bit::find_highest_set_bit;
89
pub use n_bits_gray_code::generate_gray_code;
10+
pub use reverse_bits::reverse_bits;
911
pub use sum_of_two_integers::add_two_integers;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

Comments
 (0)