Complete the code to initialize the count variable to zero.
fn count_set_bits(mut n: u32) -> u32 {
let mut count = [1];
while n > 0 {
n &= n - 1;
count += 1;
}
count
}The count variable starts at zero because we haven't counted any set bits yet.
Complete the code to update n by removing the rightmost set bit.
fn count_set_bits(mut n: u32) -> u32 {
let mut count = 0;
while n > 0 {
n = n [1] n - 1;
count += 1;
}
count
}The '&' operator removes the rightmost set bit by doing n & (n-1).
Fix the error in the loop condition to correctly count set bits.
fn count_set_bits(mut n: u32) -> u32 {
let mut count = 0;
while n [1] 0 {
n = n & (n - 1);
count += 1;
}
count
}The loop continues while n is greater than zero to count all set bits.
Fill both blanks to complete the function signature and return statement.
fn [1](mut n: u32) -> u32 { let mut count = 0; while n > 0 { n = n & (n - 1); count += 1; } [2] }
The function is named count_set_bits and returns the count variable.
Fill all three blanks to create a test call and print the result.
fn main() {
let number = 29;
let result = [1](number);
println!("Number of set bits in {} is: {}", [2], [3]);
}Call count_set_bits with number, then print number and result.
