From f7bd542734bbc42fb1041ddc1f551ebbc3f68dd2 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi Date: Tue, 9 Dec 2025 17:13:57 -0800 Subject: [PATCH 1/2] feat: add Swap Odd and Even Bits algorithm --- DIRECTORY.md | 25 ++--- src/bit_manipulation/mod.rs | 2 + src/bit_manipulation/swap_odd_even_bits.rs | 102 +++++++++++++++++++++ 3 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 src/bit_manipulation/swap_odd_even_bits.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index 20bebba4c52..d0f9f70c370 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -6,7 +6,7 @@ * [Graph Coloring](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/graph_coloring.rs) * [Hamiltonian Cycle](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/hamiltonian_cycle.rs) * [Knight Tour](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/knight_tour.rs) - * [N Queens](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/n_queens.rs) + * [N-Queens](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/n_queens.rs) * [Parentheses Generator](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/parentheses_generator.rs) * [Permutations](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/permutations.rs) * [Rat In Maze](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/rat_in_maze.rs) @@ -22,9 +22,10 @@ * [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) + * [Swap Odd and Even Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/swap_odd_even_bits.rs) * Ciphers - * [Aes](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/aes.rs) - * [Another Rot13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/another_rot13.rs) + * [AES](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/aes.rs) + * [Another ROT13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/another_rot13.rs) * [Baconian Cipher](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/baconian_cipher.rs) * [Base64](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/base64.rs) * [Blake2B](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/blake2b.rs) @@ -36,15 +37,15 @@ * [Morse Code](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/morse_code.rs) * [Polybius](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/polybius.rs) * [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) + * [ROT13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rot13.rs) * [Salsa](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/salsa.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) + * [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) * [Vigenere](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/vigenere.rs) - * [Xor](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/xor.rs) + * [XOR](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/xor.rs) * Compression * [Move To Front](https://github.com/TheAlgorithms/Rust/blob/master/src/compression/move_to_front.rs) * [Run Length Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/compression/run_length_encoding.rs) @@ -65,7 +66,7 @@ * [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) - * [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_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) * [Floyds Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/floyds_algorithm.rs) @@ -175,14 +176,14 @@ * [Lib](https://github.com/TheAlgorithms/Rust/blob/master/src/lib.rs) * Machine Learning * [Cholesky](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/cholesky.rs) - * [K Means](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/k_means.rs) + * [K-Means](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/k_means.rs) * [Linear Regression](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/linear_regression.rs) * [Logistic Regression](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/logistic_regression.rs) * Loss Function * [Average Margin Ranking Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/average_margin_ranking_loss.rs) * [Hinge Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/hinge_loss.rs) * [Huber Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/huber_loss.rs) - * [Kl Divergence Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/kl_divergence_loss.rs) + * [KL Divergence Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/kl_divergence_loss.rs) * [Mean Absolute Error Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/mean_absolute_error_loss.rs) * [Mean Squared Error Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/mean_squared_error_loss.rs) * [Negative Log Likelihood](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/negative_log_likelihood.rs) @@ -223,7 +224,7 @@ * [Frizzy Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/frizzy_number.rs) * [Gaussian Elimination](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gaussian_elimination.rs) * [Gaussian Error Linear Unit](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gaussian_error_linear_unit.rs) - * [Gcd Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gcd_of_n_numbers.rs) + * [GCD Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gcd_of_n_numbers.rs) * [Geometric Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/geometric_series.rs) * [Greatest Common Divisor](https://github.com/TheAlgorithms/Rust/blob/master/src/math/greatest_common_divisor.rs) * [Huber Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/math/huber_loss.rs) @@ -232,7 +233,7 @@ * [Interpolation](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interpolation.rs) * [Interquartile Range](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interquartile_range.rs) * [Karatsuba Multiplication](https://github.com/TheAlgorithms/Rust/blob/master/src/math/karatsuba_multiplication.rs) - * [Lcm Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/lcm_of_n_numbers.rs) + * [LCM Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/lcm_of_n_numbers.rs) * [Leaky Relu](https://github.com/TheAlgorithms/Rust/blob/master/src/math/leaky_relu.rs) * [Least Square Approx](https://github.com/TheAlgorithms/Rust/blob/master/src/math/least_square_approx.rs) * [Linear Sieve](https://github.com/TheAlgorithms/Rust/blob/master/src/math/linear_sieve.rs) @@ -325,7 +326,7 @@ * [Patience Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/patience_sort.rs) * [Pigeonhole Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/pigeonhole_sort.rs) * [Quick Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/quick_sort.rs) - * [Quick Sort 3_ways](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/quick_sort_3_ways.rs) + * [Quick Sort 3-ways](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/quick_sort_3_ways.rs) * [Radix Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/radix_sort.rs) * [Selection Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/selection_sort.rs) * [Shell Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/shell_sort.rs) diff --git a/src/bit_manipulation/mod.rs b/src/bit_manipulation/mod.rs index 00a4f77f536..c68e4191af0 100644 --- a/src/bit_manipulation/mod.rs +++ b/src/bit_manipulation/mod.rs @@ -3,9 +3,11 @@ mod highest_set_bit; mod n_bits_gray_code; mod reverse_bits; mod sum_of_two_integers; +mod swap_odd_even_bits; 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; +pub use swap_odd_even_bits::swap_odd_even_bits; diff --git a/src/bit_manipulation/swap_odd_even_bits.rs b/src/bit_manipulation/swap_odd_even_bits.rs new file mode 100644 index 00000000000..c4cd5eaaef7 --- /dev/null +++ b/src/bit_manipulation/swap_odd_even_bits.rs @@ -0,0 +1,102 @@ +/// Swaps odd and even bits in an integer. +/// +/// This function separates the even bits (0, 2, 4, 6, etc.) and odd bits (1, 3, 5, 7, etc.) +/// using bitwise AND operations, then swaps them by shifting and combining with OR. +/// +/// # Arguments +/// +/// * `num` - A 32-bit unsigned integer +/// +/// # Returns +/// +/// A new integer with odd and even bits swapped +/// +/// # Examples +/// +/// ``` +/// use rust_algorithms::bit_manipulation::swap_odd_even_bits; +/// +/// assert_eq!(swap_odd_even_bits(0), 0); +/// assert_eq!(swap_odd_even_bits(1), 2); +/// assert_eq!(swap_odd_even_bits(2), 1); +/// assert_eq!(swap_odd_even_bits(3), 3); +/// assert_eq!(swap_odd_even_bits(4), 8); +/// assert_eq!(swap_odd_even_bits(5), 10); +/// assert_eq!(swap_odd_even_bits(6), 9); +/// assert_eq!(swap_odd_even_bits(23), 43); +/// ``` +pub fn swap_odd_even_bits(num: u32) -> u32 { + // Get all even bits - 0xAAAAAAAA is a 32-bit number with all even bits set to 1 + let even_bits = num & 0xAAAAAAAA; + + // Get all odd bits - 0x55555555 is a 32-bit number with all odd bits set to 1 + let odd_bits = num & 0x55555555; + + // Right shift even bits and left shift odd bits and swap them + (even_bits >> 1) | (odd_bits << 1) +} + +/// Formats binary representation of two numbers for display. +/// +/// # Arguments +/// +/// * `before` - The original number +/// * `after` - The transformed number +/// +/// # Returns +/// +/// A formatted string showing both numbers in binary +fn show_bits(before: u32, after: u32) -> String { + format!("{:>5}: {:08b}\n{:>5}: {:08b}", before, before, after, after) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_swap_odd_even_bits() { + assert_eq!(swap_odd_even_bits(0), 0); + assert_eq!(swap_odd_even_bits(1), 2); + assert_eq!(swap_odd_even_bits(2), 1); + assert_eq!(swap_odd_even_bits(3), 3); + assert_eq!(swap_odd_even_bits(4), 8); + assert_eq!(swap_odd_even_bits(5), 10); + assert_eq!(swap_odd_even_bits(6), 9); + assert_eq!(swap_odd_even_bits(23), 43); + } + + #[test] + fn test_show_bits() { + assert_eq!( + show_bits(0, swap_odd_even_bits(0)), + " 0: 00000000\n 0: 00000000" + ); + assert_eq!( + show_bits(1, swap_odd_even_bits(1)), + " 1: 00000001\n 2: 00000010" + ); + assert_eq!( + show_bits(23, swap_odd_even_bits(23)), + " 23: 00010111\n 43: 00101011" + ); + } + + #[test] + fn test_display_output() { + let test_cases = vec![0, 1, 2, 3, 4, 23, 24]; + + for i in test_cases { + let result = show_bits(i, swap_odd_even_bits(i)); + println!("{}\n", result); + } + } +} + +fn main() { + println!("Swapping odd and even bits:\n"); + + for i in [0, 1, 2, 3, 4, 23, 24] { + println!("{}\n", show_bits(i, swap_odd_even_bits(i))); + } +} From ce070ff6622c4f870dd6851728409a813d4e9a6f Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi <41567902+AliAlimohammadi@users.noreply.github.com> Date: Tue, 9 Dec 2025 17:16:56 -0800 Subject: [PATCH 2/2] Update swap_odd_even_bits.rs --- src/bit_manipulation/swap_odd_even_bits.rs | 60 ++++++---------------- 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/src/bit_manipulation/swap_odd_even_bits.rs b/src/bit_manipulation/swap_odd_even_bits.rs index c4cd5eaaef7..51fc1c69982 100644 --- a/src/bit_manipulation/swap_odd_even_bits.rs +++ b/src/bit_manipulation/swap_odd_even_bits.rs @@ -14,7 +14,7 @@ /// # Examples /// /// ``` -/// use rust_algorithms::bit_manipulation::swap_odd_even_bits; +/// use the_algorithms_rust::bit_manipulation::swap_odd_even_bits; /// /// assert_eq!(swap_odd_even_bits(0), 0); /// assert_eq!(swap_odd_even_bits(1), 2); @@ -28,28 +28,14 @@ pub fn swap_odd_even_bits(num: u32) -> u32 { // Get all even bits - 0xAAAAAAAA is a 32-bit number with all even bits set to 1 let even_bits = num & 0xAAAAAAAA; - + // Get all odd bits - 0x55555555 is a 32-bit number with all odd bits set to 1 let odd_bits = num & 0x55555555; - + // Right shift even bits and left shift odd bits and swap them (even_bits >> 1) | (odd_bits << 1) } -/// Formats binary representation of two numbers for display. -/// -/// # Arguments -/// -/// * `before` - The original number -/// * `after` - The transformed number -/// -/// # Returns -/// -/// A formatted string showing both numbers in binary -fn show_bits(before: u32, after: u32) -> String { - format!("{:>5}: {:08b}\n{:>5}: {:08b}", before, before, after, after) -} - #[cfg(test)] mod tests { use super::*; @@ -64,39 +50,23 @@ mod tests { assert_eq!(swap_odd_even_bits(5), 10); assert_eq!(swap_odd_even_bits(6), 9); assert_eq!(swap_odd_even_bits(23), 43); + assert_eq!(swap_odd_even_bits(24), 36); } #[test] - fn test_show_bits() { - assert_eq!( - show_bits(0, swap_odd_even_bits(0)), - " 0: 00000000\n 0: 00000000" - ); - assert_eq!( - show_bits(1, swap_odd_even_bits(1)), - " 1: 00000001\n 2: 00000010" - ); - assert_eq!( - show_bits(23, swap_odd_even_bits(23)), - " 23: 00010111\n 43: 00101011" - ); - } + fn test_edge_cases() { + // All bits set + assert_eq!(swap_odd_even_bits(0xFFFFFFFF), 0xFFFFFFFF); - #[test] - fn test_display_output() { - let test_cases = vec![0, 1, 2, 3, 4, 23, 24]; - - for i in test_cases { - let result = show_bits(i, swap_odd_even_bits(i)); - println!("{}\n", result); - } + // Alternating patterns + assert_eq!(swap_odd_even_bits(0xAAAAAAAA), 0x55555555); + assert_eq!(swap_odd_even_bits(0x55555555), 0xAAAAAAAA); } -} -fn main() { - println!("Swapping odd and even bits:\n"); - - for i in [0, 1, 2, 3, 4, 23, 24] { - println!("{}\n", show_bits(i, swap_odd_even_bits(i))); + #[test] + fn test_power_of_two() { + assert_eq!(swap_odd_even_bits(16), 32); + assert_eq!(swap_odd_even_bits(32), 16); + assert_eq!(swap_odd_even_bits(64), 128); } }