diff --git a/DIRECTORY.md b/DIRECTORY.md index 46bb2a3af9f..37794c2ec3c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -31,7 +31,7 @@ * [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) * [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) + * [Kernighan](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/kernighan.rs) * [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) diff --git a/src/ciphers/kernighan.rs b/src/ciphers/kernighan.rs new file mode 100644 index 00000000000..c8144990fa2 --- /dev/null +++ b/src/ciphers/kernighan.rs @@ -0,0 +1,23 @@ +pub fn kernighan(n: u32) -> i32 { + let mut count = 0; + let mut n = n; + + while n > 0 { + n = n & (n - 1); + count += 1; + } + + count +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn count_set_bits() { + assert_eq!(kernighan(0b0000_0000_0000_0000_0000_0000_0000_1011), 3); + assert_eq!(kernighan(0b0000_0000_0000_0000_0000_0000_1000_0000), 1); + assert_eq!(kernighan(0b1111_1111_1111_1111_1111_1111_1111_1101), 31); + } +} diff --git a/src/ciphers/kerninghan.rs b/src/ciphers/kerninghan.rs deleted file mode 100644 index 4263850ff3f..00000000000 --- a/src/ciphers/kerninghan.rs +++ /dev/null @@ -1,23 +0,0 @@ -pub fn kerninghan(n: u32) -> i32 { - let mut count = 0; - let mut n = n; - - while n > 0 { - n = n & (n - 1); - count += 1; - } - - count -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn count_set_bits() { - assert_eq!(kerninghan(0b0000_0000_0000_0000_0000_0000_0000_1011), 3); - assert_eq!(kerninghan(0b0000_0000_0000_0000_0000_0000_1000_0000), 1); - assert_eq!(kerninghan(0b1111_1111_1111_1111_1111_1111_1111_1101), 31); - } -} diff --git a/src/ciphers/mod.rs b/src/ciphers/mod.rs index f7a55b0014d..1c5af11ea37 100644 --- a/src/ciphers/mod.rs +++ b/src/ciphers/mod.rs @@ -7,7 +7,7 @@ mod caesar; mod chacha; mod diffie_hellman; mod hashing_traits; -mod kerninghan; +mod kernighan; mod morse_code; mod polybius; mod rail_fence; @@ -30,7 +30,7 @@ pub use self::chacha::chacha20; pub use self::diffie_hellman::DiffieHellman; pub use self::hashing_traits::Hasher; pub use self::hashing_traits::HMAC; -pub use self::kerninghan::kerninghan; +pub use self::kernighan::kernighan; pub use self::morse_code::{decode, encode}; pub use self::polybius::{decode_ascii, encode_ascii}; pub use self::rail_fence::{rail_fence_decrypt, rail_fence_encrypt};