pub struct u256 { /* private fields */ }Expand description
The 256-bit unsigned integer type.
§Table of contents
Implementations§
Source§impl u256
§C3 (Constants, Constructors, Casts)
impl u256
§C3 (Constants, Constructors, Casts)
Sourcepub const ZERO: Self
pub const ZERO: Self
The additive identity.
§Examples
assert_eq!(x.add(u256::ZERO), x);
assert_eq!(u256::ZERO.add(x), x);Sourcepub const ONE: Self
pub const ONE: Self
The multiplicative identity.
§Examples
assert_eq!(x.mul(u256::ONE), x);
assert_eq!(u256::ONE.mul(x), x);Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this integer type.
§Examples
Basic usage:
let min = u256::from_str_or_panic("0");
assert_eq!(u256::MIN, min);Sourcepub const MAX: Self
pub const MAX: Self
The largest value that can be represented by this integer type, 2256 − 1.
§Examples
Basic usage:
let max = u256::from_str_or_panic("115792089237316195423570985008687907853269984665640564039457584007913129639935");
assert_eq!(u256::MAX, max);Sourcepub const fn from_inner(inner: [u64; 4]) -> Self
pub const fn from_inner(inner: [u64; 4]) -> Self
Constructs an integer from its inner representation, which is a low-ordered array of words. The first element of the array corresponds to the lowest 64 bits, the second to bits 64..128, and so on.
If a word (u64) is little endian, then the type’s endianess would also be the same, but it doesn’t hold in general case.
§Examples
Basic usage:
assert!(u256::from_inner([1, 2, 3, 4]).gt(u256::from_inner([4, 3, 2, 1])));Sourcepub const fn into_inner(self) -> [u64; 4]
pub const fn into_inner(self) -> [u64; 4]
Returns inner representation of self.
This function is an inverse of from_inner.
§Examples
Basic usage:
let swapped = u256::from_inner([1, 2, 3, 4]).swap_words();
assert_eq!(swapped.into_inner(), [4, 3, 2, 1]);Sourcepub const fn as_u8(self) -> u8
pub const fn as_u8(self) -> u8
Casts self to u8 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert_eq!(u256::MAX.as_u8(), u8::MAX);Sourcepub const fn as_u16(self) -> u16
pub const fn as_u16(self) -> u16
Casts self to u16 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert_eq!(u256::MAX.as_u16(), u16::MAX);Sourcepub const fn as_u32(self) -> u32
pub const fn as_u32(self) -> u32
Casts self to u32 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert_eq!(u256::MAX.as_u32(), u32::MAX);Sourcepub const fn as_u64(self) -> u64
pub const fn as_u64(self) -> u64
Casts self to u64 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert_eq!(u256::MAX.as_u64(), u64::MAX);Sourcepub const fn as_u128(self) -> u128
pub const fn as_u128(self) -> u128
Casts self to u128 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert_eq!(u256::MAX.as_u128(), u128::MAX);Sourcepub const fn as_u256(self) -> u256
pub const fn as_u256(self) -> u256
Casts self to u256 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert_eq!(u256::MAX.as_u256(), u256::MAX);Sourcepub const fn as_u384(self) -> u384
pub const fn as_u384(self) -> u384
Casts self to u384 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert!(u256::MAX.as_u384().lt(u384::MAX));Sourcepub const fn as_u512(self) -> u512
pub const fn as_u512(self) -> u512
Casts self to u512 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert!(u256::MAX.as_u512().lt(u512::MAX));Sourcepub const fn as_i256(self) -> i256
pub const fn as_i256(self) -> i256
Casts self to i256 based on semantics explained in The Rust Reference.
§Examples
Basic usage:
assert_eq!(u256::MAX.as_i256(), i256::MINUS_ONE);Sourcepub const fn from_str_radix(
src: &str,
radix: u32,
) -> Result<Self, ParseIntError>
pub const fn from_str_radix( src: &str, radix: u32, ) -> Result<Self, ParseIntError>
Converts a string slice in a given base to an integer.
The string is expected to be an optional + sign
followed by digits.
Leading and trailing whitespace represent an error.
Digits are a subset of these characters, depending on radix:
0-9a-zA-Z
§Panics
This function panics if radix is not in the range from 2 to 36.
§Examples
Basic usage:
assert_eq!(u256::from_str_radix("A", 16), Ok(u256::from_u64(10)));Sourcepub const fn from_str_radix_or_panic(src: &str, radix: u32) -> Self
pub const fn from_str_radix_or_panic(src: &str, radix: u32) -> Self
Converts a string slice in a given base to an integer.
This is the panicking variant of from_str_radix.
§Panics
This function panics if radix is not in the range from 2 to 36,
or in case of a parse error due to malformed input.
§Examples
Basic usage:
assert_eq!(u256::from_str_radix_or_panic("A", 16), u256::from_u64(10));let _ = u256::from_str_radix_or_panic("-B", 16);Sourcepub const fn from_str(src: &str) -> Result<Self, ParseIntError>
pub const fn from_str(src: &str) -> Result<Self, ParseIntError>
Converts a string slice in a base 10 to an integer.
§Examples
Basic usage:
assert_eq!(u256::from_str("98765432109876543210"), Ok(u256::from_u128(98765432109876543210)));Sourcepub const fn from_str_or_panic(src: &str) -> Self
pub const fn from_str_or_panic(src: &str) -> Self
Converts a string slice in a base 10 to an integer.
§Panics
This function panics whenever u256::from_str would have returned an Err.
§Examples
Basic usage:
assert_eq!(u256::from_str_or_panic("98765432109876543210"), u256::from_u128(98765432109876543210));let _ = u256::from_str_or_panic("a");Sourcepub const fn from_hex_str(src: &str) -> Result<Self, ParseIntError>
pub const fn from_hex_str(src: &str) -> Result<Self, ParseIntError>
Converts a string slice in a base 16 to an integer.
§Examples
Basic usage:
assert_eq!(u256::from_hex_str("98765432109876543210"), Ok(u256::from_u128(0x98765432109876543210)));Sourcepub const fn from_hex_str_or_panic(src: &str) -> Self
pub const fn from_hex_str_or_panic(src: &str) -> Self
Converts a string slice in a base 16 to an integer.
§Panics
This function panics whenever u256::from_hex_str would have returned an Err.
§Examples
Basic usage:
assert_eq!(u256::from_hex_str_or_panic("98765432109876543210"), u256::from_u128(0x98765432109876543210));let _ = u256::from_hex_str_or_panic("");Source§impl u256
§Equality and comparison
impl u256
§Equality and comparison
Sourcepub const fn equals(self, other: Self) -> bool
pub const fn equals(self, other: Self) -> bool
Tests if self == other.
§Examples
Basic usage:
let (x, y) = (u256::ZERO, u256::ONE);
assert!(x.equals(x));
assert!(!x.equals(y));Sourcepub const fn compare(self, other: Self) -> Ordering
pub const fn compare(self, other: Self) -> Ordering
Returns an Ordering
between self and other.
An implementation of a total comparison
otherwise known as Ord.
§Examples
Basic usage:
use core::cmp::Ordering;
let (x, y) = (u256::ZERO, u256::ONE);
assert_eq!(x.compare(y), Ordering::Less);
assert_eq!(y.compare(y), Ordering::Equal);
assert_eq!(y.compare(x), Ordering::Greater);Sourcepub const fn lt(self, other: Self) -> bool
pub const fn lt(self, other: Self) -> bool
Shorthand for self.compare(other).is_lt().
§Examples
Basic usage:
let (x, y) = (u256::ZERO, u256::ONE);
assert!(x.lt(y));Sourcepub const fn gt(self, other: Self) -> bool
pub const fn gt(self, other: Self) -> bool
Shorthand for self.compare(other).is_gt().
§Examples
Basic usage:
let (x, y) = (u256::ZERO, u256::ONE);
assert!(y.gt(x));Source§impl u256
§Basic arithmetic operations
impl u256
§Basic arithmetic operations
Sourcepub const fn overflowing_add(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_add(self, rhs: Self) -> (Self, bool)
Calculates self + rhs.
Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).overflowing_add(uint(2)), (uint(7), false));
assert_eq!(u256::MAX.overflowing_add(uint(1)), (uint(0), true));Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked integer addition. Computes self + rhs, returning None
if overflow occurred.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(u256::MAX.sub(uint(2)).checked_add(uint(1)), Some(u256::MAX.sub(uint(1))));
assert_eq!(u256::MAX.sub(uint(2)).checked_add(uint(3)), None);Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Saturating integer addition. Computes self + rhs, saturating at
the numeric bounds instead of overflowing.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(100).saturating_add(uint(1)), uint(101));
assert_eq!(u256::MAX.saturating_add(uint(127)), u256::MAX);Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Wrapping (modular) addition. Computes self + rhs,
wrapping around at the boundary of the type.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(200).wrapping_add(uint(55)), uint(255));
assert_eq!(uint(200).wrapping_add(u256::MAX), uint(199));Sourcepub const fn overflowing_sub(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool)
Calculates self - rhs.
Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).overflowing_sub(uint(2)), (uint(3), false));
assert_eq!(uint(0).overflowing_sub(uint(1)), (u256::MAX, true));Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked integer subtraction. Computes self - rhs, returning
None if overflow occurred.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(1).checked_sub(uint(1)), Some(uint(0)));
assert_eq!(uint(0).checked_sub(uint(1)), None);Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Saturating integer subtraction. Computes self - rhs, saturating
at the numeric bounds instead of overflowing.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(100).saturating_sub(uint(27)), uint(73));
assert_eq!(uint(13).saturating_sub(uint(127)), uint(0));Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Wrapping (modular) subtraction. Computes self - rhs,
wrapping around at the boundary of the type.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(100).wrapping_sub(uint(100)), uint(0));
assert_eq!(uint(100).wrapping_sub(u256::MAX), uint(101));Sourcepub const fn overflowing_short_mul(self, rhs: u64) -> (Self, u64)
pub const fn overflowing_short_mul(self, rhs: u64) -> (Self, u64)
Calculates the multiplication of self and rhs.
Returns a tuple of the multiplication along with u64 containing the high-order (overflowing) bits. If an overflow would have occurred then the high-order bits would contain a value not equal to 0 and the wrapped value is returned.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).overflowing_short_mul(2), (uint(10), 0));
assert_eq!(u256::MAX.overflowing_short_mul(10), (u256::MAX.sub(uint(9)), 9));Sourcepub const fn checked_short_mul(self, rhs: u64) -> Option<Self>
pub const fn checked_short_mul(self, rhs: u64) -> Option<Self>
Checked integer multiplication. Computes self * rhs, returning
None if overflow occurred.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).checked_short_mul(1), Some(uint(5)));
assert_eq!(u256::MAX.checked_short_mul(2), None);Sourcepub const fn saturating_short_mul(self, rhs: u64) -> Self
pub const fn saturating_short_mul(self, rhs: u64) -> Self
Saturating integer multiplication. Computes self * rhs,
saturating at the numeric bounds instead of overflowing.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(2).saturating_short_mul(10), uint(20));
assert_eq!(u256::MAX.saturating_short_mul(10), u256::MAX);Sourcepub const fn wrapping_short_mul(self, rhs: u64) -> Self
pub const fn wrapping_short_mul(self, rhs: u64) -> Self
Wrapping (modular) multiplication. Computes self * rhs, wrapping around at the boundary of the type.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).wrapping_short_mul(5), uint(25));
assert_eq!(u256::MAX.wrapping_short_mul(2), u256::MAX.sub(uint(1)));Sourcepub const fn overflowing_mul(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool)
Calculates the multiplication of self and rhs.
Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).overflowing_mul(uint(2)), (uint(10), false));
assert_eq!(u256::MAX.overflowing_mul(uint(10)), (u256::MAX.sub(uint(9)), true));Sourcepub const fn checked_mul(self, rhs: Self) -> Option<Self>
pub const fn checked_mul(self, rhs: Self) -> Option<Self>
Checked integer multiplication. Computes self * rhs, returning
None if overflow occurred.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).checked_mul(uint(1)), Some(uint(5)));
assert_eq!(u256::MAX.checked_mul(uint(2)), None);Sourcepub const fn saturating_mul(self, rhs: Self) -> Self
pub const fn saturating_mul(self, rhs: Self) -> Self
Saturating integer multiplication. Computes self * rhs,
saturating at the numeric bounds instead of overflowing.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(2).saturating_mul(uint(10)), uint(20));
assert_eq!(u256::MAX.saturating_mul(uint(10)), u256::MAX);Sourcepub const fn wrapping_mul(self, rhs: Self) -> Self
pub const fn wrapping_mul(self, rhs: Self) -> Self
Wrapping (modular) multiplication. Computes self * rhs, wrapping around at the boundary of the type.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(5).wrapping_mul(uint(5)), uint(25));
assert_eq!(u256::MAX.wrapping_mul(u256::MAX), uint(1));Sourcepub const fn checked_short_div(self, rhs: u64) -> Option<Self>
pub const fn checked_short_div(self, rhs: u64) -> Option<Self>
Checked integer division. Computes self / rhs, returning None
if rhs == 0.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(128).checked_short_div(2), Some(uint(64)));
assert_eq!(uint(1).checked_short_div(0), None);Sourcepub const fn checked_div(self, rhs: Self) -> Option<Self>
pub const fn checked_div(self, rhs: Self) -> Option<Self>
Checked integer division. Computes self / rhs, returning None
if rhs == 0.
§Examples
Basic usage:
let uint = u256::from_u64;
let y = u256::MAX;
let x = y.clear_bit(u256::BITS - 1);
assert_eq!(y.checked_div(x), Some(uint(2)));
assert_eq!(y.checked_div(uint(0)), None);Sourcepub const fn checked_short_rem(self, rhs: u64) -> Option<u64>
pub const fn checked_short_rem(self, rhs: u64) -> Option<u64>
Checked integer remainder. Computes self % rhs, returning None
if rhs == 0.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(128).checked_short_rem(2), Some(0));
assert_eq!(uint(1).checked_short_rem(0), None);Sourcepub const fn checked_rem(self, rhs: Self) -> Option<Self>
pub const fn checked_rem(self, rhs: Self) -> Option<Self>
Checked integer remainder. Computes self % rhs, returning None
if rhs == 0.
§Examples
Basic usage:
let uint = u256::from_u64;
let y = u256::MAX;
let x = y.clear_bit(u256::BITS - 1);
assert_eq!(y.checked_rem(x), Some(uint(1)));
assert_eq!(y.checked_rem(uint(0)), None);Source§impl u256
§Extended arithmetic operations
impl u256
§Extended arithmetic operations
Sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self in an overflowing fashion.
Returns !self + 1 using wrapping operations to return the value
that represents the negation of this unsigned value. Note that for
positive unsigned values overflow always occurs, but negating 0 does
not overflow.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0).overflowing_neg(), (uint(0), false));
assert_eq!(uint(2).overflowing_neg(), (uint(2).not().add(uint(1)), true));Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Checked negation. Computes -self, returning None unless self == 0.
Note that negating any positive integer will overflow.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0).checked_neg(), Some(uint(0)));
assert_eq!(uint(1).checked_neg(), None);Sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self,
wrapping around at the boundary of the type.
Since unsigned types do not have negative equivalents,
all applications of this function will wrap (except for -0).
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0).wrapping_neg(), uint(0));
assert_eq!(uint(2).wrapping_neg(), uint(0).wrapping_sub(uint(2)));Sourcepub const fn abs_diff(self, other: Self) -> Self
pub const fn abs_diff(self, other: Self) -> Self
Computes the absolute difference between self and other.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(100).abs_diff(uint(80)), uint(20));
assert_eq!(uint(100).abs_diff(uint(110)), uint(10));Sourcepub const fn full_mul(self, rhs: Self) -> (Self, Self)
pub const fn full_mul(self, rhs: Self) -> (Self, Self)
Calculates the complete product self * rhs
without the possibility to overflow.
This returns the low-order (wrapping) bits and the high-order (overflow) bits of the result as two separate values, in that order.
§Examples
Basic usage:
assert_eq!(u256::MAX.full_mul(u256::MAX), (u256::ONE, u256::MAX.sub(u256::ONE)));Sourcepub const fn checked_short_divrem(self, rhs: u64) -> Option<(Self, u64)>
pub const fn checked_short_divrem(self, rhs: u64) -> Option<(Self, u64)>
Checked integer division and remainder. Computes self.short_divrem(rhs),
returning None if rhs == 0.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(128).checked_short_divrem(2), Some((uint(64), 0)));
assert_eq!(uint(1).checked_short_divrem(0), None);Sourcepub const fn short_divrem(self, rhs: u64) -> (Self, u64)
pub const fn short_divrem(self, rhs: u64) -> (Self, u64)
Calculates the division and remainder of self and rhs.
Slightly more efficient variant of (self.div(rhs), self.rem(rhs))
provided for convenience.
§Panics
This function will panic if rhs is 0.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(128).short_divrem(2), (uint(64), 0));let _ = u256::ONE.short_divrem(0);Sourcepub const fn checked_divrem(self, rhs: Self) -> Option<(Self, Self)>
pub const fn checked_divrem(self, rhs: Self) -> Option<(Self, Self)>
Checked integer division and remainder. Computes self.divrem(rhs),
returning None if rhs == 0.
§Examples
Basic usage:
let uint = u256::from_u64;
let y = u256::MAX;
let x = y.clear_bit(u256::BITS - 1);
assert_eq!(y.checked_divrem(x), Some((uint(2), uint(1))));
assert_eq!(y.checked_divrem(uint(0)), None);Sourcepub const fn divrem(self, rhs: Self) -> (Self, Self)
pub const fn divrem(self, rhs: Self) -> (Self, Self)
Calculates the division and remainder of self and rhs.
Slightly more efficient variant of (self.div(rhs), self.rem(rhs))
provided for convenience.
§Panics
This function will panic if rhs is 0.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(8).divrem(uint(3)), (uint(2), uint(2)));let _ = u256::ONE.divrem(u256::ZERO);Sourcepub const fn overflowing_pow(self, exp: u32) -> (Self, bool)
pub const fn overflowing_pow(self, exp: u32) -> (Self, bool)
Raises self to the power of exp, using exponentiation by squaring.
Returns a tuple of the exponentiation along with a bool indicating whether an overflow happened.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(3).overflowing_pow(5), (uint(243), false));
assert_eq!(uint(2).overflowing_pow(512), (uint(0), true));Sourcepub const fn checked_pow(self, exp: u32) -> Option<Self>
pub const fn checked_pow(self, exp: u32) -> Option<Self>
Checked exponentiation. Computes self.pow(exp), returning None if
overflow occurred.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(2).checked_pow(5), Some(uint(32)));
assert_eq!(u256::MAX.checked_pow(2), None);Sourcepub const fn saturating_pow(self, exp: u32) -> Self
pub const fn saturating_pow(self, exp: u32) -> Self
Saturating integer exponentiation. Computes self.pow(exp),
saturating at the numeric bounds instead of overflowing.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(4).saturating_pow(3), uint(64));
assert_eq!(uint(3).saturating_pow(324), u256::MAX);Sourcepub const fn wrapping_pow(self, exp: u32) -> Self
pub const fn wrapping_pow(self, exp: u32) -> Self
Wrapping (modular) exponentiation. Computes self.pow(exp),
wrapping around at the boundary of the type.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(3).wrapping_pow(5), uint(243));
assert_eq!(uint(2).wrapping_pow(512), uint(0));Sourcepub const fn overflowing_next_power_of_two(self) -> (Self, bool)
pub const fn overflowing_next_power_of_two(self) -> (Self, bool)
Returns the tuple pair of smallest power of two greater than or equal
to self along with a bool indicating whether an overflow happened.
If the next power of two is greater than the type’s maximum value,
the return value is wrapped to 0.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(2).overflowing_next_power_of_two(), (uint(2), false));
assert_eq!(uint(3).overflowing_next_power_of_two(), (uint(4), false));
assert_eq!(u256::MAX.overflowing_next_power_of_two(), (uint(0), true));Sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
pub const fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to self.
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:
let uint = u256::from_u64;
assert_eq!(uint(2).checked_next_power_of_two(), Some(uint(2)));
assert_eq!(uint(3).checked_next_power_of_two(), Some(uint(4)));
assert_eq!(u256::MAX.checked_next_power_of_two(), None);Sourcepub const fn wrapping_next_power_of_two(self) -> Self
pub const fn wrapping_next_power_of_two(self) -> Self
Returns the smallest power of two greater than or equal to self.
If the next power of two is greater than the type’s maximum value,
the return value is wrapped to 0.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(2).wrapping_next_power_of_two(), uint(2));
assert_eq!(uint(3).wrapping_next_power_of_two(), uint(4));
assert_eq!(u256::MAX.wrapping_next_power_of_two(), uint(0));Sourcepub const fn next_power_of_two(self) -> Self
pub const fn next_power_of_two(self) -> Self
Returns the smallest power of two greater than or equal to self.
When return value overflows (i.e., self > (1 << (N-1)) for type
uN), it panics in debug mode and the return value is wrapped to 0 in
release mode (the only situation in which method can return 0).
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0).next_power_of_two(), uint(1));
assert_eq!(uint(1).next_power_of_two(), uint(1));
assert_eq!(uint(2).next_power_of_two(), uint(2));
assert_eq!(uint(3).next_power_of_two(), uint(4));Source§impl u256
§Bit manipulation
impl u256
§Bit manipulation
Sourcepub const fn bit(self, bit: u32) -> bool
pub const fn bit(self, bit: u32) -> bool
Returns the state of ith bit.
§Panics
This function panics if bit >= Self::BITS.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0b011001).bit(4), true);
assert_eq!(uint(0b011001).bit(5), false);Sourcepub const fn clear_bit(self, bit: u32) -> Self
pub const fn clear_bit(self, bit: u32) -> Self
Returns the integer based of off self but with the ith bit set to 0.
§Panics
This function panics if bit >= Self::BITS.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0b011001).clear_bit(4), uint(0b001001));
assert_eq!(uint(0b011001).clear_bit(5), uint(0b011001));Sourcepub const fn toggle_bit(self, bit: u32) -> Self
pub const fn toggle_bit(self, bit: u32) -> Self
Flips the ith bit of self.
§Panics
This function panics if bit >= Self::BITS.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0b011001).toggle_bit(4), uint(0b001001));
assert_eq!(uint(0b011001).toggle_bit(5), uint(0b111001));Sourcepub const fn set_bit(self, bit: u32) -> Self
pub const fn set_bit(self, bit: u32) -> Self
Returns the integer based of off self but with the ith bit set to 1.
§Panics
This function panics if bit >= Self::BITS.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0b011001).set_bit(4), uint(0b011001));
assert_eq!(uint(0b011001).set_bit(5), uint(0b111001));Sourcepub const fn not(self) -> Self
pub const fn not(self) -> Self
Flips each bit of self.
§Examples
Basic usage:
let x = u256::from_u64(0b011001).not();
assert!(!x.bit(0) && !x.bit(3) && !x.bit(4));
assert!(x.bit(1) && x.bit(2) && (5..u256::BITS).all(|i| x.bit(i)));Sourcepub const fn bitand(self, rhs: Self) -> Self
pub const fn bitand(self, rhs: Self) -> Self
Computes bitwise and between self and rhs.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0b011001).bitand(uint(0b110011)), uint(0b010001));Sourcepub const fn bitor(self, rhs: Self) -> Self
pub const fn bitor(self, rhs: Self) -> Self
Computes bitwise or between self and rhs.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0b011001).bitor(uint(0b110011)), uint(0b111011));Sourcepub const fn bitxor(self, rhs: Self) -> Self
pub const fn bitxor(self, rhs: Self) -> Self
Computes bitwise exclusive or between self and rhs.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0b011001).bitxor(uint(0b110011)), uint(0b101010));Sourcepub const fn overflowing_shl(self, rhs: u32) -> (Self, bool)
pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool)
Shifts self left by rhs bits.
Returns a tuple of the shifted version of self along with a boolean
indicating whether the shift value was larger than or equal to the
number of bits. If the shift value is too large, then value is
masked (N-1) where N is the number of bits, and this value is then
used to perform the shift.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0x1).overflowing_shl(4), (uint(0x10), false));
assert_eq!(uint(0x1).overflowing_shl(1540), (uint(0x10), true));Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Checked shift left. Computes self << rhs, returning None
if rhs is larger than or equal to the number of bits in self.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0x1).checked_shl(4), Some(uint(0x10)));
assert_eq!(uint(0x1).checked_shl(256), None);Sourcepub const fn saturating_shl(self, rhs: u32) -> Self
pub const fn saturating_shl(self, rhs: u32) -> Self
Saturating shift left. Computes self << rhs, returning 0
if rhs is larger than or equal to the number of bits in self.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0x1).saturating_shl(4), uint(0x10));
assert_eq!(uint(0x1).saturating_shl(256), uint(0));Sourcepub const fn wrapping_shl(self, rhs: u32) -> Self
pub const 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.
Note that this is not the same as a rotate-left; the
RHS of a wrapping shift-left is restricted to the range
of the type, rather than the bits shifted out of the LHS
being returned to the other end. The wider integer
types all implement a rotate_left function,
which may be what you want instead.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(1).wrapping_shl(7), uint(128));
assert_eq!(uint(1).wrapping_shl(1536), uint(1));Sourcepub const fn overflowing_shr(self, rhs: u32) -> (Self, bool)
pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool)
Shifts self right by rhs bits.
Returns a tuple of the shifted version of self along with a boolean
indicating whether the shift value was larger than or equal to the
number of bits. If the shift value is too large, then value is
masked (N-1) where N is the number of bits, and this value is then
used to perform the shift.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0x10).overflowing_shr(4), (uint(0x1), false));
assert_eq!(uint(0x10).overflowing_shr(1540), (uint(0x1), true));Sourcepub const fn checked_shr(self, rhs: u32) -> Option<Self>
pub const fn checked_shr(self, rhs: u32) -> Option<Self>
Checked shift right. Computes self >> rhs, returning None
if rhs is larger than or equal to the number of bits in self.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0x10).checked_shr(4), Some(uint(0x1)));
assert_eq!(uint(0x10).checked_shr(256), None);Sourcepub const fn saturating_shr(self, rhs: u32) -> Self
pub const fn saturating_shr(self, rhs: u32) -> Self
Saturating shift right. Computes self >> rhs, returning 0
if rhs is larger than or equal to the number of bits in self.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(0x10).saturating_shr(4), uint(0x1));
assert_eq!(uint(0x10).saturating_shl(256), uint(0));Sourcepub const fn wrapping_shr(self, rhs: u32) -> Self
pub const fn wrapping_shr(self, rhs: u32) -> Self
Panic-free bitwise shift-right; 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.
Note that this is not the same as a rotate-right; the
RHS of a wrapping shift-right is restricted to the range
of the type, rather than the bits shifted out of the LHS
being returned to the other end. The wider integer
types all implement a rotate_right function,
which may be what you want instead.
§Examples
Basic usage:
let uint = u256::from_u64;
assert_eq!(uint(128).wrapping_shr(7), uint(1));
assert_eq!(uint(128).wrapping_shr(1536), uint(128));Sourcepub const fn count_ones(self) -> u32
pub const fn count_ones(self) -> u32
Returns the number of ones in the binary representation of self.
§Examples
Basic usage:
let n = u256::from_u64(0b01001100);
assert_eq!(n.count_ones(), 3);Sourcepub const fn count_zeros(self) -> u32
pub const fn count_zeros(self) -> u32
Returns the number of zeros in the binary representation of self.
§Examples
Basic usage:
assert_eq!(u256::MAX.count_zeros(), 0);Sourcepub const fn leading_ones(self) -> u32
pub const fn leading_ones(self) -> u32
Returns the number of leading ones in the binary representation of self.
§Examples
Basic usage:
let n = u256::MAX.shr(2).not();
assert_eq!(n.leading_ones(), 2);Sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self.
§Examples
Basic usage:
let n = u256::MAX.shr(2);
assert_eq!(n.leading_zeros(), 2);Sourcepub const fn trailing_ones(self) -> u32
pub const fn trailing_ones(self) -> u32
Returns the number of trailing ones in the binary representation
of self.
§Examples
Basic usage:
let n = u256::from_u64(0b1010111);
assert_eq!(n.trailing_ones(), 3);Sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self.
§Examples
Basic usage:
let n = u256::from_u64(0b0101000);
assert_eq!(n.trailing_zeros(), 3);Sourcepub const fn rotate_left(self, rhs: u32) -> Self
pub const fn rotate_left(self, rhs: u32) -> Self
Shifts the bits to the left by a specified amount, n,
wrapping the truncated bits to the end of the resulting integer.
Please note this isn’t the same operation as the << shifting operator!
§Examples
Basic usage:
let n = u256::from_inner([2, 3, 0, 1]);
let m = u256::from_inner([2, 4, 6, 0]);
assert_eq!(n.rotate_left(65), m);Sourcepub const fn rotate_right(self, rhs: u32) -> Self
pub const fn rotate_right(self, rhs: u32) -> Self
Shifts the bits to the right by a specified amount, n,
wrapping the truncated bits to the beginning of the resulting
integer.
Please note this isn’t the same operation as the >> shifting operator!
§Examples
Basic usage:
let n = u256::from_inner([2, 4, 6, 0]);
let m = u256::from_inner([2, 3, 0, 1]);
assert_eq!(n.rotate_right(65), m);Sourcepub const fn swap_words(self) -> Self
pub const fn swap_words(self) -> Self
Reverses the word order of the integer.
Here ‘word’ means an underlying primitive integer type
that the current implementation relies upon. It effectively
reverses the array of words returned by into_inner.
§Examples
Basic usage:
let n = u256::from_inner([1, 2, 3, 4]);
let m = u256::from_inner([4, 3, 2, 1]);
assert_eq!(n.swap_words(), m);Sourcepub const fn swap_bytes(self) -> Self
pub const fn swap_bytes(self) -> Self
Reverses the byte order of the integer.
§Examples
Basic usage:
let n = u256::from_hex_str_or_panic("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20");
let m = u256::from_hex_str_or_panic("201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a090807060504030201");
assert_eq!(n.swap_bytes(), m);Sourcepub const fn reverse_bits(self) -> Self
pub const fn reverse_bits(self) -> Self
Reverses the order of bits in the integer. The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
§Examples
Basic usage:
let n = u256::from_hex_str_or_panic("1234567890123456789012345678901234567890123456789012345678901234");
let m = u256::from_hex_str_or_panic("2c48091e6a2c48091e6a2c48091e6a2c48091e6a2c48091e6a2c48091e6a2c48");
assert_eq!(n.reverse_bits(), m);Trait Implementations§
Source§impl AddAssign<&u256> for u256
impl AddAssign<&u256> for u256
Source§fn add_assign(&mut self, rhs: &u256)
fn add_assign(&mut self, rhs: &u256)
+= operation. Read moreSource§impl AddAssign for u256
impl AddAssign for u256
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl BitAndAssign<&u256> for u256
impl BitAndAssign<&u256> for u256
Source§fn bitand_assign(&mut self, rhs: &Self)
fn bitand_assign(&mut self, rhs: &Self)
&= operation. Read moreSource§impl BitAndAssign for u256
impl BitAndAssign for u256
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&= operation. Read moreSource§impl BitOrAssign<&u256> for u256
impl BitOrAssign<&u256> for u256
Source§fn bitor_assign(&mut self, rhs: &Self)
fn bitor_assign(&mut self, rhs: &Self)
|= operation. Read moreSource§impl BitOrAssign for u256
impl BitOrAssign for u256
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|= operation. Read moreSource§impl BitXorAssign<&u256> for u256
impl BitXorAssign<&u256> for u256
Source§fn bitxor_assign(&mut self, rhs: &Self)
fn bitxor_assign(&mut self, rhs: &Self)
^= operation. Read moreSource§impl BitXorAssign for u256
impl BitXorAssign for u256
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^= operation. Read moreSource§impl DecStr for u256
Available on crate feature serde only.
impl DecStr for u256
serde only.fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl DefaultModule for u256
Available on crate feature serde only.
impl DefaultModule for u256
serde only.fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'de> Deserialize<'de> for u256
Available on crate feature serde only.
impl<'de> Deserialize<'de> for u256
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl DivAssign<&u256> for u256
impl DivAssign<&u256> for u256
Source§fn div_assign(&mut self, rhs: &u256)
fn div_assign(&mut self, rhs: &u256)
/= operation. Read moreSource§impl DivAssign<&u64> for u256
impl DivAssign<&u64> for u256
Source§fn div_assign(&mut self, rhs: &u64)
fn div_assign(&mut self, rhs: &u64)
/= operation. Read moreSource§impl DivAssign<u64> for u256
impl DivAssign<u64> for u256
Source§fn div_assign(&mut self, rhs: u64)
fn div_assign(&mut self, rhs: u64)
/= operation. Read moreSource§impl DivAssign for u256
impl DivAssign for u256
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<const SIGN_AWARE: bool, const UPPER_HEX: bool, const STRICT: bool> HexStr<SIGN_AWARE, UPPER_HEX, STRICT> for u256
Available on crate feature serde only.
impl<const SIGN_AWARE: bool, const UPPER_HEX: bool, const STRICT: bool> HexStr<SIGN_AWARE, UPPER_HEX, STRICT> for u256
serde only.fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl InnerArray for u256
Available on crate feature serde only.
impl InnerArray for u256
serde only.fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl MulAssign<&u256> for u256
impl MulAssign<&u256> for u256
Source§fn mul_assign(&mut self, rhs: &u256)
fn mul_assign(&mut self, rhs: &u256)
*= operation. Read moreSource§impl MulAssign<&u64> for u256
impl MulAssign<&u64> for u256
Source§fn mul_assign(&mut self, rhs: &u64)
fn mul_assign(&mut self, rhs: &u64)
*= operation. Read moreSource§impl MulAssign<u64> for u256
impl MulAssign<u64> for u256
Source§fn mul_assign(&mut self, rhs: u64)
fn mul_assign(&mut self, rhs: u64)
*= operation. Read moreSource§impl MulAssign for u256
impl MulAssign for u256
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl Ord for u256
impl Ord for u256
Source§impl PartialOrd for u256
impl PartialOrd for u256
Source§impl RemAssign<&u256> for u256
impl RemAssign<&u256> for u256
Source§fn rem_assign(&mut self, rhs: &u256)
fn rem_assign(&mut self, rhs: &u256)
%= operation. Read moreSource§impl RemAssign<&u64> for u256
impl RemAssign<&u64> for u256
Source§fn rem_assign(&mut self, rhs: &u64)
fn rem_assign(&mut self, rhs: &u64)
%= operation. Read moreSource§impl RemAssign<u64> for u256
impl RemAssign<u64> for u256
Source§fn rem_assign(&mut self, rhs: u64)
fn rem_assign(&mut self, rhs: u64)
%= operation. Read moreSource§impl RemAssign for u256
impl RemAssign for u256
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read moreSource§impl ShlAssign<&u32> for u256
impl ShlAssign<&u32> for u256
Source§fn shl_assign(&mut self, rhs: &u32)
fn shl_assign(&mut self, rhs: &u32)
<<= operation. Read moreSource§impl ShlAssign<u32> for u256
impl ShlAssign<u32> for u256
Source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<= operation. Read moreSource§impl ShrAssign<&u32> for u256
impl ShrAssign<&u32> for u256
Source§fn shr_assign(&mut self, rhs: &u32)
fn shr_assign(&mut self, rhs: &u32)
>>= operation. Read moreSource§impl ShrAssign<u32> for u256
impl ShrAssign<u32> for u256
Source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>= operation. Read moreSource§impl SubAssign<&u256> for u256
impl SubAssign<&u256> for u256
Source§fn sub_assign(&mut self, rhs: &u256)
fn sub_assign(&mut self, rhs: &u256)
-= operation. Read moreSource§impl SubAssign for u256
impl SubAssign for u256
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreimpl Copy for u256
impl Eq for u256
impl StructuralPartialEq for u256
Auto Trait Implementations§
impl Freeze for u256
impl RefUnwindSafe for u256
impl Send for u256
impl Sync for u256
impl Unpin for u256
impl UnwindSafe for u256
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<H> HexStrDefault for Hwhere
H: HexStr,
impl<H> HexStrDefault for Hwhere
H: HexStr,
Source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
serde only.Source§fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
serde only.