Skip to content

Commit 825abe8

Browse files
authored
count trailing ones (#38)
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent d5062de commit 825abe8

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ pub fn index(depth: usize, offset: usize) -> usize {
3737
/// ```
3838
#[inline]
3939
pub fn depth(i: usize) -> usize {
40-
let mut depth = 0;
41-
let mut i = i;
42-
while is_odd(i) {
43-
i >>= 1;
44-
depth += 1;
45-
}
46-
depth
40+
// Count trailing `1`s of the binary representation of the number.
41+
(!i).trailing_zeros() as usize
4742
}
4843

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

0 commit comments

Comments
 (0)