std-method

Integer count_ones and count_zeros

Count enabled flags and unused bits in a fixed-width mask. Expected stdout (not executed): 4 4 Common mistake: Forgetting that leading zero bits are included in the width.

Formal Syntax & Structure

rust syntaxrust
x.count_ones() -> u32; x.count_zeros() -> u32

Working Example

Example Snippetrust
fn main() {    let flags = 0b1010_0101u8;    println!("{} {}", flags.count_ones(), flags.count_zeros());}
Rust Reference