-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_of_1_bits.rs
47 lines (38 loc) · 1.04 KB
/
number_of_1_bits.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// Write a function that takes the binary representation of an unsigned
/// integer and returns the number of `1` bits it has (also known as the
/// Hamming weight).
struct Solution;
impl Solution {
// Should be hamming_weight, but the problem uses
// hammingWeight
#[allow(non_snake_case)]
pub fn hammingWeight(n: u32) -> i32 {
let mut result = 0;
for byte in n.to_ne_bytes() {
result += byte.count_ones();
}
result as i32
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let n = 0b00000000000000000000000000001011;
let result = Solution::hammingWeight(n);
assert_eq!(result, 3);
}
#[test]
fn example_2() {
let n = 0b00000000000000000000000010000000;
let result = Solution::hammingWeight(n);
assert_eq!(result, 1);
}
#[test]
fn example_3() {
let n = 0b11111111111111111111111111111101;
let result = Solution::hammingWeight(n);
assert_eq!(result, 31);
}
}