Skip to content

Commit 1b03215

Browse files
committed
count trailing ones
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent dc2038f commit 1b03215

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

src/lib.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,8 @@ pub fn index(depth: usize, offset: usize) -> usize {
3535
/// assert_eq!(flat_tree::depth(4), 0);
3636
/// ```
3737
pub fn depth(i: usize) -> usize {
38-
let mut depth = 0;
39-
let mut i = i;
40-
while is_odd(i) {
41-
i >>= 1;
42-
depth += 1;
43-
}
44-
depth
38+
// Count trailing `1`s of the binary representation of the number.
39+
(!i).trailing_zeros() as usize
4540
}
4641

4742
/// Returns the offset of a node with a depth.

0 commit comments

Comments
 (0)