std-method

Integer leading and trailing bit counts

Inspect runs of bits from the most or least significant end of an integer. Expected stdout (not executed): 2 3 2 2 Common mistake: Confusing trailing zeros with the total number of zeros.

Formal Syntax & Structure

rust syntaxrust
x.leading_zeros(); x.trailing_zeros(); x.leading_ones(); x.trailing_ones()

Working Example

Example Snippetrust
fn main() {    let n = 0b0010_1000u8;    println!("{} {}", n.leading_zeros(), n.trailing_zeros());    println!("{} {}", 0b1100_0011u8.leading_ones(), 0b1100_0011u8.trailing_ones());}
Rust Reference