Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from TheAlgorithms:master #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 1 commit into AlgorithmAndLeetCode:master from TheAlgorithms:master
Dec 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions DIRECTORY.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* [NQueens](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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
* [BTree](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)
Expand Down Expand Up @@ -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)
* [KMeans](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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/bit_manipulation/mod.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
72 changes: 72 additions & 0 deletions src/bit_manipulation/swap_odd_even_bits.rs
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/// 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 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);
/// 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)
}

#[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);
assert_eq!(swap_odd_even_bits(24), 36);
}

#[test]
fn test_edge_cases() {
// All bits set
assert_eq!(swap_odd_even_bits(0xFFFFFFFF), 0xFFFFFFFF);

// Alternating patterns
assert_eq!(swap_odd_even_bits(0xAAAAAAAA), 0x55555555);
assert_eq!(swap_odd_even_bits(0x55555555), 0xAAAAAAAA);
}

#[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);
}
}

AltStyle によって変換されたページ (->オリジナル) /