File tree Expand file tree Collapse file tree 3 files changed +26
-25
lines changed Expand file tree Collapse file tree 3 files changed +26
-25
lines changed Original file line number Diff line number Diff line change 1+ pub fn kernighan ( n : u32 ) -> i32 {
2+ let mut count = 0 ;
3+ let mut n = n;
4+
5+ while n > 0 {
6+ n = n & ( n - 1 ) ;
7+ count += 1 ;
8+ }
9+
10+ count
11+ }
12+
13+ #[ cfg( test) ]
14+ mod tests {
15+ use super :: * ;
16+
17+ #[ test]
18+ fn count_set_bits ( ) {
19+ assert_eq ! ( kernighan( 0b0000_0000_0000_0000_0000_0000_0000_1011 ) , 3 ) ;
20+ assert_eq ! ( kernighan( 0b0000_0000_0000_0000_0000_0000_1000_0000 ) , 1 ) ;
21+ assert_eq ! ( kernighan( 0b1111_1111_1111_1111_1111_1111_1111_1101 ) , 31 ) ;
22+ }
23+ }
24+
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ mod caesar;
77mod chacha;
88mod diffie_hellman;
99mod hashing_traits;
10- mod kerninghan ;
10+ mod kernighan ;
1111mod morse_code;
1212mod polybius;
1313mod rail_fence;
@@ -30,7 +30,7 @@ pub use self::chacha::chacha20;
3030pub use self :: diffie_hellman:: DiffieHellman ;
3131pub use self :: hashing_traits:: Hasher ;
3232pub use self :: hashing_traits:: HMAC ;
33- pub use self :: kerninghan :: kerninghan ;
33+ pub use self :: kernighan :: kernighan ;
3434pub use self :: morse_code:: { decode, encode} ;
3535pub use self :: polybius:: { decode_ascii, encode_ascii} ;
3636pub use self :: rail_fence:: { rail_fence_decrypt, rail_fence_encrypt} ;
You can’t perform that action at this time.
0 commit comments