Skip to content

Commit 057127c

Browse files
committed
update isolate_highest_one for NonZero<T>
1 parent 2f7620a commit 057127c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

library/core/src/num/nonzero.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,12 +660,15 @@ macro_rules! nonzero_integer {
660660
without modifying the original"]
661661
#[inline(always)]
662662
pub const fn isolate_highest_one(self) -> Self {
663-
let n = self.get() & (((1 as $Int) << (<$Int>::BITS - 1)).wrapping_shr(self.leading_zeros()));
664-
665663
// SAFETY:
666664
// `self` is non-zero, so masking to preserve only the most
667665
// significant set bit will result in a non-zero `n`.
668-
unsafe { NonZero::new_unchecked(n) }
666+
// and self.leading_zeros() is always < $INT::BITS since
667+
// at least one of the bits in the number is not zero
668+
unsafe {
669+
let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
670+
NonZero::new_unchecked(bit as $Int)
671+
}
669672
}
670673

671674
/// Returns `self` with only the least significant bit set.

0 commit comments

Comments
 (0)