From 1b03215585f708a703dcd63a7dbd469df2efd423 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Fri, 2 Nov 2018 12:40:12 +0100 Subject: [PATCH] count trailing ones Signed-off-by: Yoshua Wuyts --- src/lib.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 65aca1e..b0603ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,13 +35,8 @@ pub fn index(depth: usize, offset: usize) -> usize { /// assert_eq!(flat_tree::depth(4), 0); /// ``` pub fn depth(i: usize) -> usize { - let mut depth = 0; - let mut i = i; - while is_odd(i) { - i >>= 1; - depth += 1; - } - depth + // Count trailing `1`s of the binary representation of the number. + (!i).trailing_zeros() as usize } /// Returns the offset of a node with a depth.