pub struct Int<const N: usize>(/* private fields */);Expand description
Generic Signed integer type composed of 64-bit digits, of arbitrary fixed size which must be known at compile time.
Digits are stored in little endian (the least significant digit first).
This integer type aims to exactly replicate the behaviours of Rust’s
built-in signed integer types: u8, u16, u32, u64, u128 and
usize.
The const generic parameter N is the number of 64-bit digits that are
stored.
Implementations§
Source§impl<const N: usize> Int<N>
Checked arithmetic methods which act on self: self.checked_.... Each method cannot panic and returns an Option<Self>. None is returned when overflow would have occurred or there was an attempt to divide by zero or calculate a remainder with a divisor of zero.
impl<const N: usize> Int<N>
Checked arithmetic methods which act on self: self.checked_.... Each method cannot panic and returns an Option<Self>. None is returned when overflow would have occurred or there was an attempt to divide by zero or calculate a remainder with a divisor of zero.
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_div(self, rhs: Self) -> Option<Self>
pub const fn checked_div(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_div_euclid(self, rhs: Self) -> Option<Self>
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_rem(self, rhs: Self) -> Option<Self>
pub const fn checked_rem(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self>
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_shr(self, rhs: u32) -> Option<Self>
pub const fn checked_shr(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_pow(self, pow: u32) -> Option<Self>
pub const fn checked_pow(self, pow: u32) -> Option<Self>
Sourcepub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Source§impl<const N: usize> Int<N>
impl<const N: usize> Int<N>
Sourcepub const fn checked_add_unsigned(self, rhs: UInt<N>) -> Option<Self>
pub const fn checked_add_unsigned(self, rhs: UInt<N>) -> Option<Self>
Sourcepub const fn checked_sub_unsigned(self, rhs: UInt<N>) -> Option<Self>
pub const fn checked_sub_unsigned(self, rhs: UInt<N>) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: Self) -> Option<Self>
pub const fn checked_mul(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_ilog2(self) -> Option<u32>
pub const fn checked_ilog2(self) -> Option<u32>
Sourcepub const fn checked_ilog10(self) -> Option<u32>
pub const fn checked_ilog10(self) -> Option<u32>
Sourcepub const fn checked_ilog(self, base: Self) -> Option<u32>
pub const fn checked_ilog(self, base: Self) -> Option<u32>
Sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Source§impl<const N: usize> Int<N>
impl<const N: usize> Int<N>
Sourcepub const fn eq(&self, other: &Self) -> bool
pub const fn eq(&self, other: &Self) -> bool
Tests for self and other values to be equal, and is used by ==.
Sourcepub const fn ne(&self, other: &Self) -> bool
pub const fn ne(&self, other: &Self) -> bool
Tests for self and other values to be not equal, and is used by !=.
Sourcepub const fn cmp(&self, other: &Self) -> Ordering
pub const fn cmp(&self, other: &Self) -> Ordering
This method returns an core::cmp::Ordering between self and other.
By convention, self.cmp(&other) returns the ordering matching the expression
self <operator> other if true.
Sourcepub const fn max(self, other: Self) -> Self
pub const fn max(self, other: Self) -> Self
Compares and returns the maximum of two values.
Returns the second argument if the comparison determines them to be equal.
Sourcepub const fn min(self, other: Self) -> Self
pub const fn min(self, other: Self) -> Self
Compares and returns the minimum of two values.
Returns the first argument if the comparison determines them to be equal.
Sourcepub const fn clamp(self, min: Self, max: Self) -> Self
pub const fn clamp(self, min: Self, max: Self) -> Self
Restrict a value to a certain interval.
Returns max if self is greater than max, and min if self is less than min.Otherwise this returns self.# Panics
Panics if min > max.
Sourcepub const fn lt(&self, other: &Self) -> bool
pub const fn lt(&self, other: &Self) -> bool
Tests less than (for self and other) and is used by the < operator.
Sourcepub const fn le(&self, other: &Self) -> bool
pub const fn le(&self, other: &Self) -> bool
Tests less than or equal to (for self and other) and is used by the <= operator.
Source§impl<const N: usize> Int<N>
impl<const N: usize> Int<N>
Sourcepub const fn parse_str(s: &str) -> Self
pub const fn parse_str(s: &str) -> Self
Parse I256 from string using hexadecimal, binary or decimal base.
§Panics
This function will panic if I256 can’t be constructed
from a given string.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
assert_eq!(I256::parse_str("0b1"), i256!(1));
assert_eq!(I256::parse_str("0xA"), i256!(10));
assert_eq!(I256::parse_str("12345"), i256!(12345));
Sourcepub const fn parse_str_radix(s: &str, radix: u32) -> Self
pub const fn parse_str_radix(s: &str, radix: u32) -> Self
Parse I256 from string using 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 will panic if I256 can’t be constructed
from a given string or if radix is not in the range from 2 to 36 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
assert_eq!(I256::parse_str_radix("A", 16), i256!(10));
Sourcepub const fn from_str(s: &str) -> Result<Self, ParseError>
pub const fn from_str(s: &str) -> Result<Self, ParseError>
Try parse I256 from string using hexadecimal, binary or decimal base.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
assert_eq!(I256::from_str("0b1"), Ok(i256!(1)));
assert_eq!(I256::from_str("0xA"), Ok(i256!(10)));
assert_eq!(I256::from_str("12345"), Ok(i256!(12345)));
Sourcepub const fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseError>
pub const fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseError>
Try parse I256 from string using 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 will panic if radix is not in the range from 2 to 36 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
assert_eq!(I256::from_str_radix("A", 16), Ok(i256!(10)));
Sourcepub const fn parse_bytes(buf: &[u8], radix: u32) -> Option<Self>
pub const fn parse_bytes(buf: &[u8], radix: u32) -> Option<Self>
Converts a byte slice in a given base to an I256 integer.
The input slice must contain ascii/utf8 characters in [0-9a-zA-Z].
This function is equivalent to the from_str_radix function for a string slice equivalent to the byte slice and the same radix.
Returns None if the conversion of the byte slice to string slice fails or if a digit is larger than or equal to the given radix, otherwise the integer is wrapped in Some.
§Panics
This function will panic if radix is not in the range from 2 to 36 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let src = "394857hdgfjhsnkg947dgfjkeita";
assert_eq!(I256::from_str_radix(src, 32).ok(), I256::parse_bytes(src.as_bytes(), 32));
Sourcepub const fn from_radix_be(buf: &[u8], radix: u32) -> Option<Self>
pub const fn from_radix_be(buf: &[u8], radix: u32) -> Option<Self>
Converts a slice of big-endian digits in the given radix to an I256 integer.
Each u8 of the slice is interpreted as one digit of base radix of the number, so this function will return None if any digit is greater than or equal to radix, otherwise the integer is wrapped in Some.
§Panics
This function will panic if radix is not in the range from 2 to 256 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(3459874852685);
let digits = n.to_radix_be(12);
assert_eq!(Some(n), I256::from_radix_be(&digits, 12));
Sourcepub const fn from_radix_le(buf: &[u8], radix: u32) -> Option<Self>
pub const fn from_radix_le(buf: &[u8], radix: u32) -> Option<Self>
Converts a slice of little-endian digits in the given radix to an I256 integer.
Each u8 of the slice is interpreted as one digit of base radix of the number, so this function will return None if any digit is greater than or equal to radix, otherwise the integer is wrapped in Some.
§Panics
This function will panic if radix is not in the range from 2 to 256 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(10983745987895);
let digits = n.to_radix_le(15);
assert_eq!(Some(n), I256::from_radix_le(&digits, 15));
Sourcepub const fn from_usize(uint: usize) -> Result<Self, ParseError>
pub const fn from_usize(uint: usize) -> Result<Self, ParseError>
Sourcepub const fn from_isize(int: isize) -> Self
pub const fn from_isize(int: isize) -> Self
Source§impl<const N: usize> Int<N>
impl<const N: usize> Int<N>
Sourcepub fn to_str_radix(&self, radix: u32) -> String
pub fn to_str_radix(&self, radix: u32) -> String
Returns the I256 integer as a string in the given radix.
§Panics
This function will panic if radix is not in the range from 2 to 36 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let src = "934857djkfghhkdfgbf9345hdfkh";
let n = I256::from_str_radix(src, 36).unwrap();
assert_eq!(n.to_str_radix(36), src);
Sourcepub fn to_radix_be(&self, radix: u32) -> Vec<u8> ⓘ
pub fn to_radix_be(&self, radix: u32) -> Vec<u8> ⓘ
Returns the I256 integer in the given base in big-endian digit order.
§Panics
This function will panic if radix is not in the range from 2 to 256 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let digits = &[3, 55, 60, 100, 5, 0, 5, 88];
let n = I256::from_radix_be(digits, 120).unwrap();
assert_eq!(n.to_radix_be(120), digits);
Sourcepub fn to_radix_le(&self, radix: u32) -> Vec<u8> ⓘ
pub fn to_radix_le(&self, radix: u32) -> Vec<u8> ⓘ
Returns the I256 integer in the given base in little-endian digit order.
§Panics
This function will panic if radix is not in the range from 2 to 256 inclusive.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let digits = &[1, 67, 88, 200, 55, 68, 87, 120, 178];
let n = I256::from_radix_le(digits, 250).unwrap();
assert_eq!(n.to_radix_le(250), digits);
Source§impl<const N: usize> Int<N>
Methods which convert a Int to and from data stored in different endianness.
impl<const N: usize> Int<N>
Methods which convert a Int to and from data stored in different endianness.
Sourcepub const fn from_be(x: Self) -> Self
pub const fn from_be(x: Self) -> Self
Converts an integer from big endian to the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.from_be.
Sourcepub const fn from_le(x: Self) -> Self
pub const fn from_le(x: Self) -> Self
Converts an integer from little endian to the target’s endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.from_le.
Sourcepub const fn to_be(self) -> Self
pub const fn to_be(self) -> Self
Converts self from big endian to the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.to_be.
Sourcepub const fn to_le(self) -> Self
pub const fn to_le(self) -> Self
Converts self from little endian to the target’s endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.to_le.
Sourcepub const fn from_be_slice(slice: &[u8]) -> Option<Self>
pub const fn from_be_slice(slice: &[u8]) -> Option<Self>
Create an integer value from a slice of bytes in big endian.
The value is wrapped in an Option as the integer represented by the slice of bytes may represent an integer too large to be represented by the type.
If the length of the slice is shorter than Self::BYTES, the slice is padded with zeros or ones at the start so that it’s length equals Self::BYTES. It is padded with ones if the bytes represent a negative integer, otherwise it is padded with zeros.
If the length of the slice is longer than Self::BYTES, None will be returned, unless the bytes represent a non-negative integer and leading zeros from the slice can be removed until the length of the slice equals Self::BYTES, or if the bytes represent a negative integer and leading ones from the slice can be removed until the length of the slice equals Self::BYTES.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.from_be_slice.
Sourcepub const fn from_le_slice(slice: &[u8]) -> Option<Self>
pub const fn from_le_slice(slice: &[u8]) -> Option<Self>
Creates an integer value from a slice of bytes in little endian.
The value is wrapped in an Option as the bytes may represent an integer too large to be represented by the type.
If the length of the slice is shorter than Self::BYTES, the slice is padded with zeros or ones at the start so that it’s length equals Self::BYTES. It is padded with ones if the bytes represent a negative integer, otherwise it is padded with zeros.
If the length of the slice is longer than Self::BYTES, None will be returned, unless the bytes represent a non-negative integer and leading zeros from the slice can be removed until the length of the slice equals Self::BYTES, or if the bytes represent a negative integer and leading ones from the slice can be removed until the length of the slice equals Self::BYTES.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.from_le_slice.
Source§impl<const N: usize> Int<N>
impl<const N: usize> Int<N>
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.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.count_ones.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let a = i256!(7);
assert_eq!(a.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.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.count_zeros.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let a = I256::NEG_ONE;
assert_eq!(a.count_zeros(), 0);
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.
Depending on what you’re doing with the value, you might also be interested in the Self::ilog2 function which returns a consistent number, even if the type widens.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.leading_zeros.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let a = I256::MIN;
assert_eq!(a.leading_zeros(), 0);
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.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.trailing_zeros.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let a = i256!(4);
assert_eq!(a.trailing_zeros(), 2);
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.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.leading_ones.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let a = I256::NEG_ONE;
assert_eq!(a.leading_ones(), 256);
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.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.trailing_ones.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let a = i256!(3);
assert_eq!(a.trailing_ones(), 2);
Sourcepub const fn rotate_left(self, n: u32) -> Self
pub const fn rotate_left(self, n: 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!
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.rotate_left.
Sourcepub const fn rotate_right(self, n: u32) -> Self
pub const fn rotate_right(self, n: 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!
self.rotate_right(n) is equivalent to self.rotate_left(Self::BITS - n).
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.rotate_right.
Sourcepub const fn swap_bytes(self) -> Self
pub const fn swap_bytes(self) -> Self
Reverses the byte order of the integer.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.swap_bytes.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(0x12345678901234567890123456789012);
assert_eq!(n.swap_bytes().swap_bytes(), n);
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.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.reverse_bits.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(0x12345678901234567890123456789012);
assert_eq!(n.reverse_bits().reverse_bits(), n);
Sourcepub const fn pow(self, exp: u32) -> Self
pub const fn pow(self, exp: u32) -> Self
Raises self to the power of exp, using exponentiation by squaring.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.pow.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(3);
assert_eq!(n.pow(5), i256!(243));
Sourcepub const fn div_euclid(self, rhs: Self) -> Self
pub const fn div_euclid(self, rhs: Self) -> Self
Performs Euclidean division.
Since, for the positive integers, all common definitions of division are equal, this is exactly equal to self / rhs.
§Panics
This function will panic if rhs is zero.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.div_euclid.
Sourcepub const fn rem_euclid(self, rhs: Self) -> Self
pub const fn rem_euclid(self, rhs: Self) -> Self
Calculates the least remainder of self (mod rhs).
Since, for the positive integers, all common definitions of division are equal, this is exactly equal to self % rhs.
§Panics
This function will panic if rhs is zero.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.rem_euclid.
Sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
Returns true if and only if self == 2^k for some integer k.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.is_power_of_two.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(8);
assert!(n.is_power_of_two());
let m = i256!(90);
assert!(!m.is_power_of_two());
Sourcepub const fn midpoint(self, rhs: Self) -> Self
pub const fn midpoint(self, rhs: Self) -> Self
Calculates the midpoint (average) between self and rhs.
midpoint(a, b) is (a + b) / 2 as if it were performed in a sufficiently-large unsigned integral type. This implies that the result is always rounded towards zero and that no overflow will ever occur.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.midpoint.
Sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
§Panics
This function will panic if self is zero.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.ilog2.
Sourcepub const fn ilog(self, base: Self) -> u32
pub const fn ilog(self, base: Self) -> u32
Returns the logarithm of the number with respect to an arbitrary base, rounded down.
This method might not be optimized owing to implementation details; ilog2 can produce results more efficiently for base 2, and ilog10 can produce results more efficiently for base 10.# Panics
This function will panic if self is zero, or if base is less than 2.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.ilog.
Sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Find integer log10(x) of an integer.
fastnum use the most efficient algorithm based on relationship:
log10(x) = log2(x)/log2(10)
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.ilog10.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(150);
assert_eq!(n.ilog10(), 2);
Sourcepub const fn next_multiple_of(self, rhs: Self) -> Self
pub const fn next_multiple_of(self, rhs: Self) -> Self
Calculates the smallest value greater than or equal to self that is a multiple of rhs.
§Panics
This function will panic if rhs is zero.
§Overflow behavior
On overflow, this function will panic if overflow checks are enabled (default in debug mode) and wrap if overflow checks are disabled (default in release mode).
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.next_multiple_of.
Sourcepub const fn div_floor(self, rhs: Self) -> Self
pub const fn div_floor(self, rhs: Self) -> Self
Calculates the quotient of self and rhs, rounding the result towards negative infinity.
§Panics
This function will panic if rhs is zero.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.div_floor.
Sourcepub const fn div_ceil(self, rhs: Self) -> Self
pub const fn div_ceil(self, rhs: Self) -> Self
Calculates the quotient of self and rhs, rounding the result towards positive infinity.
§Panics
This function will panic if rhs is zero.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.div_ceil.
Sourcepub const fn bits(&self) -> u32
pub const fn bits(&self) -> u32
Returns the smallest number of bits necessary to represent self.
This is equal to the size of the type in bits minus the leading zeros of self.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
assert_eq!(i256!(0b1111001010100).bits(), 13);
assert_eq!(I256::ZERO.bits(), 0);
Sourcepub const fn bit(&self, b: u32) -> bool
pub const fn bit(&self, b: u32) -> bool
Returns a boolean representing the bit in the given position (true if the bit is set).
The least significant bit is at index 0, the most significant bit is at index Self::BITS - 1.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let n = i256!(0b001010100101010101);
assert!(n.bit(0));
assert!(!n.bit(1));
assert!(!n.bit(I256::BITS - 1));
Source§impl<const N: usize> Int<N>
impl<const N: usize> Int<N>
Sourcepub const fn from_bits(bits: UInt<N>) -> Self
pub const fn from_bits(bits: UInt<N>) -> Self
Creates an integer with bits as its underlying representation in two’s complement.
Sourcepub const fn to_bits(self) -> UInt<N>
pub const fn to_bits(self) -> UInt<N>
This simply returns the underlying representation of the integer in two’s complement, as an unsigned integer.
Sourcepub const fn cast_unsigned(self) -> UInt<N>
pub const fn cast_unsigned(self) -> UInt<N>
Returns the bit pattern of self reinterpreted as an unsigned integer of the same size.
This produces the same result as an as cast, but ensures that the bit-width remains the same.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.cast_unsigned.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
let a = i256!(-1);
assert_eq!(a.cast_unsigned(), U256::MAX);
Sourcepub const fn unsigned_abs(self) -> UInt<N>
pub const fn unsigned_abs(self) -> UInt<N>
Computes the absolute value of self as unsigned integer without panicking.let a = i256!(-50);
let b = u256!(50);
assert_eq!(a.unsigned_abs(), b);
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.unsigned_abs.
Sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Computes the absolute value of self.
§Overflow behavior
The absolute value of i128::MIN cannot be represented as an i128, 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 i128::MIN without a panic. If you do not want this behavior, consider using unsigned_abs instead.
let a = i256!(-50); let b = i256!(50);
assert_eq!(a.abs(), b);
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.abs.
Sourcepub const fn signum(self) -> Self
pub const fn signum(self) -> Self
Returns a number representing sign of self.
0if the number is zero1if the number is positive-1if the number is negative
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.signum.
§Examples
Please note that this example is shared between integer types.
Which explains why I256 is used here.
use fastnum::*;
assert_eq!(i256!(10).signum(), i256!(1));
assert_eq!(i256!(0).signum(), i256!(0));
assert_eq!(i256!(-10).signum(), i256!(-1));
Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the number is zero or negative.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.is_positive.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the number is zero or positive.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.is_negative.
Sourcepub const fn abs_diff(self, other: Self) -> UInt<N>
pub const fn abs_diff(self, other: Self) -> UInt<N>
Computes the absolute difference between self and other.
See also: https://doc.rust-lang.org/std/primitive.i64.html#method.abs_diff.
Sourcepub const fn from_digits(digits: [u64; N]) -> Self
pub const fn from_digits(digits: [u64; N]) -> Self
Creates a new unsigned integer from the given array of digits. Digits are stored as little endian (least significant digit first).
Source§impl<const N: usize> Int<N>
Overflowing arithmetic methods which act on self: self.overflowing_.... Each method returns a tuple of type (Self, bool) where the first item of the tuple is the result of the calculation truncated to the number of bits of self, and the second item is a boolean which indicates whether overflow occurred (i.e. if the number of bits of the result of the calculation exceeded the number of bits of self).
impl<const N: usize> Int<N>
Overflowing arithmetic methods which act on self: self.overflowing_.... Each method returns a tuple of type (Self, bool) where the first item of the tuple is the result of the calculation truncated to the number of bits of self, and the second item is a boolean which indicates whether overflow occurred (i.e. if the number of bits of the result of the calculation exceeded the number of bits of self).
Sourcepub const fn overflowing_add(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_add(self, rhs: Self) -> (Self, bool)
Sourcepub const fn overflowing_sub(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool)
Sourcepub const fn overflowing_div(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool)
Sourcepub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool)
Sourcepub const fn overflowing_rem(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool)
Sourcepub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool)
Sourcepub const fn overflowing_shl(self, rhs: u32) -> (Self, bool)
pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool)
Sourcepub const fn overflowing_shr(self, rhs: u32) -> (Self, bool)
pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool)
Sourcepub const fn overflowing_pow(self, pow: u32) -> (Self, bool)
pub const fn overflowing_pow(self, pow: u32) -> (Self, bool)
Sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Source§impl<const N: usize> Int<N>
Overflowing arithmetic methods which act on self: self.overflowing_.... Each method returns a tuple of type (Self, bool) where the first item of the tuple is the result of the calculation truncated to the number of bits of self, and the second item is a boolean which indicates whether overflow occurred (i.e. if the number of bits of the result of the calculation exceeded the number of bits of self).
impl<const N: usize> Int<N>
Overflowing arithmetic methods which act on self: self.overflowing_.... Each method returns a tuple of type (Self, bool) where the first item of the tuple is the result of the calculation truncated to the number of bits of self, and the second item is a boolean which indicates whether overflow occurred (i.e. if the number of bits of the result of the calculation exceeded the number of bits of self).
Sourcepub const fn overflowing_mul(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool)
Sourcepub const fn overflowing_add_unsigned(self, rhs: UInt<N>) -> (Self, bool)
pub const fn overflowing_add_unsigned(self, rhs: UInt<N>) -> (Self, bool)
Sourcepub const fn overflowing_sub_unsigned(self, rhs: UInt<N>) -> (Self, bool)
pub const fn overflowing_sub_unsigned(self, rhs: UInt<N>) -> (Self, bool)
Sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Source§impl<const N: usize> Int<N>
Saturating arithmetic methods which act on self: self.saturating_.... For each method, if overflow or underflow occurs, the largest or smallest value that can be represented by Self is returned instead.
impl<const N: usize> Int<N>
Saturating arithmetic methods which act on self: self.saturating_.... For each method, if overflow or underflow occurs, the largest or smallest value that can be represented by Self is returned instead.
Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Sourcepub const fn saturating_mul(self, rhs: Self) -> Self
pub const fn saturating_mul(self, rhs: Self) -> Self
Sourcepub const fn saturating_div(self, rhs: Self) -> Self
pub const fn saturating_div(self, rhs: Self) -> Self
Sourcepub const fn saturating_pow(self, exp: u32) -> Self
pub const fn saturating_pow(self, exp: u32) -> Self
Source§impl<const N: usize> Int<N>
Saturating arithmetic methods which act on self: self.saturating_.... For each method, if overflow or underflow occurs, the largest or smallest value that can be represented by Self is returned instead.
impl<const N: usize> Int<N>
Saturating arithmetic methods which act on self: self.saturating_.... For each method, if overflow or underflow occurs, the largest or smallest value that can be represented by Self is returned instead.
Sourcepub const fn saturating_add_unsigned(self, rhs: UInt<N>) -> Self
pub const fn saturating_add_unsigned(self, rhs: UInt<N>) -> Self
Sourcepub const fn saturating_sub_unsigned(self, rhs: UInt<N>) -> Self
pub const fn saturating_sub_unsigned(self, rhs: UInt<N>) -> Self
Sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Source§impl<const N: usize> Int<N>
Strict arithmetic methods which act on self: self.strict_.... Each method will always panic if overflow/underflow occurs (i.e. when the checked equivalent would return None), regardless of whether overflow checks are enabled.
impl<const N: usize> Int<N>
Strict arithmetic methods which act on self: self.strict_.... Each method will always panic if overflow/underflow occurs (i.e. when the checked equivalent would return None), regardless of whether overflow checks are enabled.
Sourcepub const fn strict_add(self, rhs: Self) -> Self
pub const fn strict_add(self, rhs: Self) -> Self
Sourcepub const fn strict_sub(self, rhs: Self) -> Self
pub const fn strict_sub(self, rhs: Self) -> Self
Sourcepub const fn strict_div(self, rhs: Self) -> Self
pub const fn strict_div(self, rhs: Self) -> Self
Sourcepub const fn strict_div_euclid(self, rhs: Self) -> Self
pub const fn strict_div_euclid(self, rhs: Self) -> Self
Sourcepub const fn strict_rem(self, rhs: Self) -> Self
pub const fn strict_rem(self, rhs: Self) -> Self
Sourcepub const fn strict_rem_euclid(self, rhs: Self) -> Self
pub const fn strict_rem_euclid(self, rhs: Self) -> Self
Sourcepub const fn strict_shl(self, rhs: u32) -> Self
pub const fn strict_shl(self, rhs: u32) -> Self
Sourcepub const fn strict_shr(self, rhs: u32) -> Self
pub const fn strict_shr(self, rhs: u32) -> Self
Sourcepub const fn strict_pow(self, exp: u32) -> Self
pub const fn strict_pow(self, exp: u32) -> Self
Sourcepub const fn strict_neg(self) -> Self
pub const fn strict_neg(self) -> Self
Source§impl<const N: usize> Int<N>
Strict arithmetic methods which act on self: self.strict_.... Each method will always panic if overflow/underflow occurs (i.e. when the checked equivalent would return None), regardless of whether overflow checks are enabled.
impl<const N: usize> Int<N>
Strict arithmetic methods which act on self: self.strict_.... Each method will always panic if overflow/underflow occurs (i.e. when the checked equivalent would return None), regardless of whether overflow checks are enabled.
Sourcepub const fn strict_abs(self) -> Self
pub const fn strict_abs(self) -> Self
Sourcepub const fn strict_mul(self, rhs: Self) -> Self
pub const fn strict_mul(self, rhs: Self) -> Self
Sourcepub const fn strict_add_unsigned(self, rhs: UInt<N>) -> Self
pub const fn strict_add_unsigned(self, rhs: UInt<N>) -> Self
Sourcepub const fn strict_sub_unsigned(self, rhs: UInt<N>) -> Self
pub const fn strict_sub_unsigned(self, rhs: UInt<N>) -> Self
impl<const N: usize> Int<N>
Widening arithmetic methods which act on self: self.widening_.... Each method returns of the calculation without the possibility to overflow.
Source§impl<const N: usize> Int<N>
Saturating arithmetic methods which act on self: self.saturating_.... For each method, if overflow or underflow occurs, the largest or smallest value that can be represented by Self is returned instead.
impl<const N: usize> Int<N>
Saturating arithmetic methods which act on self: self.saturating_.... For each method, if overflow or underflow occurs, the largest or smallest value that can be represented by Self is returned instead.
Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Sourcepub const fn wrapping_mul(self, rhs: Self) -> Self
pub const fn wrapping_mul(self, rhs: Self) -> Self
Sourcepub const fn wrapping_div(self, rhs: Self) -> Self
pub const fn wrapping_div(self, rhs: Self) -> Self
Sourcepub const fn wrapping_div_euclid(self, rhs: Self) -> Self
pub const fn wrapping_div_euclid(self, rhs: Self) -> Self
Sourcepub const fn wrapping_rem(self, rhs: Self) -> Self
pub const fn wrapping_rem(self, rhs: Self) -> Self
Sourcepub const fn wrapping_rem_euclid(self, rhs: Self) -> Self
pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self
Sourcepub const fn wrapping_shl(self, rhs: u32) -> Self
pub const fn wrapping_shl(self, rhs: u32) -> Self
Sourcepub const fn wrapping_shr(self, rhs: u32) -> Self
pub const fn wrapping_shr(self, rhs: u32) -> Self
Sourcepub const fn wrapping_pow(self, pow: u32) -> Self
pub const fn wrapping_pow(self, pow: u32) -> Self
Sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Source§impl<const N: usize> Int<N>
Wrapping arithmetic methods which act on self: self.wrapping_.... Each method returns of the calculation truncated to the number of bits of self (i.e. they each return the first item in the tuple returned by their overflowing equivalent).
impl<const N: usize> Int<N>
Wrapping arithmetic methods which act on self: self.wrapping_.... Each method returns of the calculation truncated to the number of bits of self (i.e. they each return the first item in the tuple returned by their overflowing equivalent).
Sourcepub const fn wrapping_add_unsigned(self, rhs: UInt<N>) -> Self
pub const fn wrapping_add_unsigned(self, rhs: UInt<N>) -> Self
Sourcepub const fn wrapping_sub_unsigned(self, rhs: UInt<N>) -> Self
pub const fn wrapping_sub_unsigned(self, rhs: UInt<N>) -> Self
Sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Trait Implementations§
Source§impl<const N: usize> AddAssign for Int<N>
impl<const N: usize> AddAssign for Int<N>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<'de, const N: usize> Deserialize<'de> for Int<N>
impl<'de, const N: usize> Deserialize<'de> for Int<N>
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<const N: usize> DivAssign for Int<N>
impl<const N: usize> DivAssign for Int<N>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<const N: usize> MulAssign for Int<N>
impl<const N: usize> MulAssign for Int<N>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<const N: usize> Ord for Int<N>
impl<const N: usize> Ord for Int<N>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const N: usize> PartialOrd for Int<N>
impl<const N: usize> PartialOrd for Int<N>
Source§impl<const N: usize> RemAssign for Int<N>
impl<const N: usize> RemAssign for Int<N>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read moreSource§impl<const N: usize> ShlAssign<i128> for Int<N>
impl<const N: usize> ShlAssign<i128> for Int<N>
Source§fn shl_assign(&mut self, rhs: i128)
fn shl_assign(&mut self, rhs: i128)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<i16> for Int<N>
impl<const N: usize> ShlAssign<i16> for Int<N>
Source§fn shl_assign(&mut self, rhs: i16)
fn shl_assign(&mut self, rhs: i16)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<i32> for Int<N>
impl<const N: usize> ShlAssign<i32> for Int<N>
Source§fn shl_assign(&mut self, rhs: i32)
fn shl_assign(&mut self, rhs: i32)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<i64> for Int<N>
impl<const N: usize> ShlAssign<i64> for Int<N>
Source§fn shl_assign(&mut self, rhs: i64)
fn shl_assign(&mut self, rhs: i64)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<i8> for Int<N>
impl<const N: usize> ShlAssign<i8> for Int<N>
Source§fn shl_assign(&mut self, rhs: i8)
fn shl_assign(&mut self, rhs: i8)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<isize> for Int<N>
impl<const N: usize> ShlAssign<isize> for Int<N>
Source§fn shl_assign(&mut self, rhs: isize)
fn shl_assign(&mut self, rhs: isize)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<u128> for Int<N>
impl<const N: usize> ShlAssign<u128> for Int<N>
Source§fn shl_assign(&mut self, rhs: u128)
fn shl_assign(&mut self, rhs: u128)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<u16> for Int<N>
impl<const N: usize> ShlAssign<u16> for Int<N>
Source§fn shl_assign(&mut self, rhs: u16)
fn shl_assign(&mut self, rhs: u16)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<u32> for Int<N>
impl<const N: usize> ShlAssign<u32> for Int<N>
Source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<u64> for Int<N>
impl<const N: usize> ShlAssign<u64> for Int<N>
Source§fn shl_assign(&mut self, rhs: u64)
fn shl_assign(&mut self, rhs: u64)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<u8> for Int<N>
impl<const N: usize> ShlAssign<u8> for Int<N>
Source§fn shl_assign(&mut self, rhs: u8)
fn shl_assign(&mut self, rhs: u8)
<<= operation. Read moreSource§impl<const N: usize> ShlAssign<usize> for Int<N>
impl<const N: usize> ShlAssign<usize> for Int<N>
Source§fn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
<<= operation. Read moreSource§impl<const N: usize> ShrAssign<i128> for Int<N>
impl<const N: usize> ShrAssign<i128> for Int<N>
Source§fn shr_assign(&mut self, rhs: i128)
fn shr_assign(&mut self, rhs: i128)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<i16> for Int<N>
impl<const N: usize> ShrAssign<i16> for Int<N>
Source§fn shr_assign(&mut self, rhs: i16)
fn shr_assign(&mut self, rhs: i16)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<i32> for Int<N>
impl<const N: usize> ShrAssign<i32> for Int<N>
Source§fn shr_assign(&mut self, rhs: i32)
fn shr_assign(&mut self, rhs: i32)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<i64> for Int<N>
impl<const N: usize> ShrAssign<i64> for Int<N>
Source§fn shr_assign(&mut self, rhs: i64)
fn shr_assign(&mut self, rhs: i64)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<i8> for Int<N>
impl<const N: usize> ShrAssign<i8> for Int<N>
Source§fn shr_assign(&mut self, rhs: i8)
fn shr_assign(&mut self, rhs: i8)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<isize> for Int<N>
impl<const N: usize> ShrAssign<isize> for Int<N>
Source§fn shr_assign(&mut self, rhs: isize)
fn shr_assign(&mut self, rhs: isize)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<u128> for Int<N>
impl<const N: usize> ShrAssign<u128> for Int<N>
Source§fn shr_assign(&mut self, rhs: u128)
fn shr_assign(&mut self, rhs: u128)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<u16> for Int<N>
impl<const N: usize> ShrAssign<u16> for Int<N>
Source§fn shr_assign(&mut self, rhs: u16)
fn shr_assign(&mut self, rhs: u16)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<u32> for Int<N>
impl<const N: usize> ShrAssign<u32> for Int<N>
Source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<u64> for Int<N>
impl<const N: usize> ShrAssign<u64> for Int<N>
Source§fn shr_assign(&mut self, rhs: u64)
fn shr_assign(&mut self, rhs: u64)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<u8> for Int<N>
impl<const N: usize> ShrAssign<u8> for Int<N>
Source§fn shr_assign(&mut self, rhs: u8)
fn shr_assign(&mut self, rhs: u8)
>>= operation. Read moreSource§impl<const N: usize> ShrAssign<usize> for Int<N>
impl<const N: usize> ShrAssign<usize> for Int<N>
Source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
>>= operation. Read moreSource§impl<const N: usize> SubAssign for Int<N>
impl<const N: usize> SubAssign for Int<N>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more