Skip to content
Merged
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
34 changes: 34 additions & 0 deletions number-of-1-bits/bus710.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = Solution::hamming_weight(11);
assert_eq!(result, 3);
let result = Solution::hamming_weight(128);
assert_eq!(result, 1);
let result = Solution::hamming_weight(2147483645);
assert_eq!(result, 30);
}
}

#[derive(Debug)]
struct Solution;

#[allow(dead_code)]
impl Solution {
pub fn hamming_weight(n: i32) -> i32 {
let mut cnt: i32 = 0;
let mut target_bit_holder: i32;

for i in 0..32 {
target_bit_holder = n & (0x1 << i);
if target_bit_holder != 0 {
cnt += 1;
}
}

cnt
}
}
Loading