Rollup merge of #136910 - okaneco:sig_ones, r=thomcc
Implement feature `isolate_most_least_significant_one` for integer types Accepted ACP - https://github.com/rust-lang/libs-team/issues/467 Tracking issue - #136909 Implement ACP for functions that isolate the most significant set bit and least significant set bit on unsigned, signed, and `NonZero` integers. Add function `isolate_most_significant_one` Add function `isolate_least_significant_one` --- This PR adds the following impls ```rust impl {u8, u16, u32, u64, u128, usize} { const fn isolate_most_significant_one(self) -> Self; const fn isolate_least_significant_one(self) -> Self; } impl {i8, i16, i32, i64, i128, isize} { const fn isolate_most_significant_one(self) -> Self; const fn isolate_least_significant_one(self) -> Self; } impl NonZeroT { const fn isolate_most_significant_one(self) -> Self; const fn isolate_least_significant_one(self) -> Self; } ``` Example behavior ```rust assert_eq!(u8::isolate_most_significant_one(0b01100100), 0b01000000); assert_eq!(u8::isolate_least_significant_one(0b01100100), 0b00000100); ```
This commit is contained in:
@@ -213,6 +213,52 @@ macro_rules! uint_impl {
|
||||
(!self).trailing_zeros()
|
||||
}
|
||||
|
||||
/// Returns `self` with only the most significant bit set, or `0` if
|
||||
/// the input is `0`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(isolate_most_least_significant_one)]
|
||||
///
|
||||
#[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
|
||||
///
|
||||
/// assert_eq!(n.isolate_most_significant_one(), 0b_01000000);
|
||||
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_most_significant_one(), 0);")]
|
||||
/// ```
|
||||
#[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline(always)]
|
||||
pub const fn isolate_most_significant_one(self) -> Self {
|
||||
self & (((1 as $SelfT) << (<$SelfT>::BITS - 1)).wrapping_shr(self.leading_zeros()))
|
||||
}
|
||||
|
||||
/// Returns `self` with only the least significant bit set, or `0` if
|
||||
/// the input is `0`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(isolate_most_least_significant_one)]
|
||||
///
|
||||
#[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
|
||||
///
|
||||
/// assert_eq!(n.isolate_least_significant_one(), 0b_00000100);
|
||||
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_least_significant_one(), 0);")]
|
||||
/// ```
|
||||
#[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline(always)]
|
||||
pub const fn isolate_least_significant_one(self) -> Self {
|
||||
self & self.wrapping_neg()
|
||||
}
|
||||
|
||||
/// Returns the bit pattern of `self` reinterpreted as a signed integer of the same size.
|
||||
///
|
||||
/// This produces the same result as an `as` cast, but ensures that the bit-width remains
|
||||
|
||||
Reference in New Issue
Block a user