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
use crate::popcount_table_const_8_bit_usize::POPCOUNT;
pub fn popcount(mut n: usize) -> usize {
const MASK: usize = 0xff;
let mut cnt = 0;
while n > 0 {
cnt += POPCOUNT[n & MASK];
n >>= 8;
}
cnt
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let cases = [
(0b1010, 2),
(0b1100100, 3),
(0b001010101, 4),
(0b11111, 5),
(0b00000, 0),
];
for (n, ans) in cases {
assert_eq!(popcount(n), ans);
}
}
}