Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,15 @@ macro_rules! nonzero_integer {
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
let n = self.get() & (((1 as $Int) << (<$Int>::BITS - 1)).wrapping_shr(self.leading_zeros()));

// SAFETY:
// `self` is non-zero, so masking to preserve only the most
// significant set bit will result in a non-zero `n`.
unsafe { NonZero::new_unchecked(n) }
// and self.leading_zeros() is always < $INT::BITS since
// at least one of the bits in the number is not zero
unsafe {
let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as $Int)
}
}

/// Returns `self` with only the least significant bit set.
Expand Down
Loading