///
/// # Examples
///
+ /// Basic usage:
+ ///
/// ```
/// assert_eq!(u32::from_str_radix("A", 16), Ok(10));
/// ```
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b01001100u8;
///
/// assert_eq!(n.count_ones(), 3);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b01001100u8;
///
/// assert_eq!(n.count_zeros(), 5);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b0101000u16;
///
/// assert_eq!(n.leading_zeros(), 10);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b0101000u16;
///
/// assert_eq!(n.trailing_zeros(), 3);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0x3456789ABCDEF012u64;
///
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0xDEF0123456789ABCu64;
///
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0xEFCDAB8967452301u64;
///
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "big") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "little") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "big") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "little") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!(5u16.checked_add(65530), Some(65535));
/// assert_eq!(6u16.checked_add(65530), None);
/// ```
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!((-127i8).checked_sub(1), Some(-128));
/// assert_eq!((-128i8).checked_sub(1), None);
/// ```
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!(5u8.checked_mul(51), Some(255));
/// assert_eq!(5u8.checked_mul(52), None);
/// ```
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!((-127i8).checked_div(-1), Some(127));
/// assert_eq!((-128i8).checked_div(-1), None);
/// assert_eq!((1i8).checked_div(0), None);
/// Saturating integer addition. Computes `self + other`, saturating at
/// the numeric bounds instead of overflowing.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.saturating_add(1), 101);
+ /// assert_eq!(100i8.saturating_add(127), 127);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_add(self, other: Self) -> Self {
/// Saturating integer subtraction. Computes `self - other`, saturating
/// at the numeric bounds instead of overflowing.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.saturating_sub(127), -27);
+ /// assert_eq!((-100i8).saturating_sub(127), -128);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_sub(self, other: Self) -> Self {
/// Wrapping (modular) addition. Computes `self + other`,
/// wrapping around at the boundary of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.wrapping_add(27), 127);
+ /// assert_eq!(100i8.wrapping_add(127), -29);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_add(self, rhs: Self) -> Self {
/// Wrapping (modular) subtraction. Computes `self - other`,
/// wrapping around at the boundary of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(0i8.wrapping_sub(127), -127);
+ /// assert_eq!((-2i8).wrapping_sub(127), 127);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_sub(self, rhs: Self) -> Self {
/// Wrapping (modular) multiplication. Computes `self *
/// other`, wrapping around at the boundary of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(10i8.wrapping_mul(12), 120);
+ /// assert_eq!(11i8.wrapping_mul(12), -124);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_mul(self, rhs: Self) -> Self {
/// to `-MIN`, a positive value that is too large to represent
/// in the type. In such a case, this function returns `MIN`
/// itself.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100u8.wrapping_div(10), 10);
+ /// assert_eq!((-128i8).wrapping_div(-1), -128);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_div(self, rhs: Self) -> Self {
/// implementation artifacts make `x % y` invalid for `MIN /
/// -1` on a signed type (where `MIN` is the negative
/// minimal value). In such a case, this function returns `0`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.wrapping_rem(10), 0);
+ /// assert_eq!((-128i8).wrapping_rem(-1), 0);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_rem(self, rhs: Self) -> Self {
/// negative minimal value for the type); this is a positive
/// value that is too large to represent in the type. In such
/// a case, this function returns `MIN` itself.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.wrapping_neg(), -100);
+ /// assert_eq!((-128i8).wrapping_neg(), -128);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_neg(self) -> Self {
/// Panic-free bitwise shift-left; yields `self << mask(rhs)`,
/// where `mask` removes any high-order bits of `rhs` that
/// would cause the shift to exceed the bitwidth of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(1u8.wrapping_shl(7), 128);
+ /// assert_eq!(1u8.wrapping_shl(8), 1);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_shl(self, rhs: u32) -> Self {
/// Panic-free bitwise shift-left; yields `self >> mask(rhs)`,
/// where `mask` removes any high-order bits of `rhs` that
/// would cause the shift to exceed the bitwidth of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(128u8.wrapping_shr(7), 1);
+ /// assert_eq!(128u8.wrapping_shr(8), 128);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_shr(self, rhs: u32) -> Self {
///
/// # Examples
///
+ /// Basic usage:
+ ///
/// ```
/// let x: i32 = 2; // or any other integer type
///
/// `i32`, and attempting to calculate it will cause an overflow. This
/// means that code in debug mode will trigger a panic on this case and
/// optimized code will return `i32::min_value()` without a panic.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(10i8.abs(), 10);
+ /// assert_eq!((-10i8).abs(), 10);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn abs(self) -> Self {
/// - `0` if the number is zero
/// - `1` if the number is positive
/// - `-1` if the number is negative
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(10i8.signum(), 1);
+ /// assert_eq!(0i8.signum(), 0);
+ /// assert_eq!((-10i8).signum(), -1);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn signum(self) -> Self {
/// Returns `true` if `self` is positive and `false` if the number
/// is zero or negative.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert!(10i8.is_positive());
+ /// assert!(!(-10i8).is_positive());
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_positive(self) -> bool { self > 0 }
/// Returns `true` if `self` is negative and `false` if the number
/// is zero or positive.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert!((-10i8).is_negative());
+ /// assert!(!10i8.is_negative());
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_negative(self) -> bool { self < 0 }
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b01001100u8;
///
/// assert_eq!(n.count_ones(), 3);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b01001100u8;
///
/// assert_eq!(n.count_zeros(), 5);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b0101000u16;
///
/// assert_eq!(n.leading_zeros(), 10);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0b0101000u16;
///
/// assert_eq!(n.trailing_zeros(), 3);
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0x3456789ABCDEF012u64;
///
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0xDEF0123456789ABCu64;
///
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0xEFCDAB8967452301u64;
///
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "big") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "little") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "big") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "little") {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!(5u16.checked_add(65530), Some(65535));
/// assert_eq!(6u16.checked_add(65530), None);
/// ```
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!((-127i8).checked_sub(1), Some(-128));
/// assert_eq!((-128i8).checked_sub(1), None);
/// ```
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!(5u8.checked_mul(51), Some(255));
/// assert_eq!(5u8.checked_mul(52), None);
/// ```
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!((-127i8).checked_div(-1), Some(127));
/// assert_eq!((-128i8).checked_div(-1), None);
/// assert_eq!((1i8).checked_div(0), None);
/// Saturating integer addition. Computes `self + other`, saturating at
/// the numeric bounds instead of overflowing.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.saturating_add(1), 101);
+ /// assert_eq!(100i8.saturating_add(127), 127);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_add(self, other: Self) -> Self {
/// Saturating integer subtraction. Computes `self - other`, saturating
/// at the numeric bounds instead of overflowing.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.saturating_sub(127), -27);
+ /// assert_eq!((-100i8).saturating_sub(127), -128);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_sub(self, other: Self) -> Self {
/// Wrapping (modular) addition. Computes `self + other`,
/// wrapping around at the boundary of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.wrapping_add(27), 127);
+ /// assert_eq!(100i8.wrapping_add(127), -29);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_add(self, rhs: Self) -> Self {
/// Wrapping (modular) subtraction. Computes `self - other`,
/// wrapping around at the boundary of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(0i8.wrapping_sub(127), -127);
+ /// assert_eq!((-2i8).wrapping_sub(127), 127);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_sub(self, rhs: Self) -> Self {
/// Wrapping (modular) multiplication. Computes `self *
/// other`, wrapping around at the boundary of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(10i8.wrapping_mul(12), 120);
+ /// assert_eq!(11i8.wrapping_mul(12), -124);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_mul(self, rhs: Self) -> Self {
/// to `-MIN`, a positive value that is too large to represent
/// in the type. In such a case, this function returns `MIN`
/// itself.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100u8.wrapping_div(10), 10);
+ /// assert_eq!((-128i8).wrapping_div(-1), -128);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_div(self, rhs: Self) -> Self {
/// implementation artifacts make `x % y` invalid for `MIN /
/// -1` on a signed type (where `MIN` is the negative
/// minimal value). In such a case, this function returns `0`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.wrapping_rem(10), 0);
+ /// assert_eq!((-128i8).wrapping_rem(-1), 0);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_rem(self, rhs: Self) -> Self {
/// negative minimal value for the type); this is a positive
/// value that is too large to represent in the type. In such
/// a case, this function returns `MIN` itself.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(100i8.wrapping_neg(), -100);
+ /// assert_eq!((-128i8).wrapping_neg(), -128);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_neg(self) -> Self {
/// Panic-free bitwise shift-left; yields `self << mask(rhs)`,
/// where `mask` removes any high-order bits of `rhs` that
/// would cause the shift to exceed the bitwidth of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(1u8.wrapping_shl(7), 128);
+ /// assert_eq!(1u8.wrapping_shl(8), 1);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_shl(self, rhs: u32) -> Self {
/// Panic-free bitwise shift-left; yields `self >> mask(rhs)`,
/// where `mask` removes any high-order bits of `rhs` that
/// would cause the shift to exceed the bitwidth of the type.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(128u8.wrapping_shr(7), 1);
+ /// assert_eq!(128u8.wrapping_shr(8), 128);
+ /// ```
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_shr(self, rhs: u32) -> Self {
///
/// # Examples
///
- /// ```rust
+ /// Basic usage:
+ ///
+ /// ```
/// assert_eq!(2i32.pow(4), 16);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
}
/// Returns `true` if and only if `self == 2^k` for some `k`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert!(16u8.is_power_of_two());
+ /// assert!(!10u8.is_power_of_two());
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_power_of_two(self) -> bool {
/// Returns the smallest power of two greater than or equal to `self`.
/// Unspecified behavior on overflow.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(2u8.next_power_of_two(), 2);
+ /// assert_eq!(3u8.next_power_of_two(), 4);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn next_power_of_two(self) -> Self {
/// Returns the smallest power of two greater than or equal to `n`. If
/// the next power of two is greater than the type's maximum value,
/// `None` is returned, otherwise the power of two is wrapped in `Some`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!(2u8.checked_next_power_of_two(), Some(2));
+ /// assert_eq!(3u8.checked_next_power_of_two(), Some(4));
+ /// assert_eq!(200u8.checked_next_power_of_two(), None);
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn checked_next_power_of_two(self) -> Option<Self> {
let npot = self.next_power_of_two();