Skip to content
Open
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
2 changes: 1 addition & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions src/ciphers/kernighan.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 0 additions & 23 deletions src/ciphers/kerninghan.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/ciphers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down