Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

Counting bits Kernighan style

// Counting bits Kernighan style
fn bit_count(mut n: usize) -> usize {
    let mut cnt = 0;
    while n > 0 {
        n &= n - 1;
        cnt += 1;
    }
    cnt
}

fn main() {
    println!("Bit count = {} ", bit_count(254));
}
 
PREVIOUS NEXT
Tagged: #Counting #bits #Kernighan #style
ADD COMMENT
Topic
Name
3+9 =