pub struct BitUint<T: Unsigned + PrimInt, const N: u32>(/* private fields */);
Expand description
BitUint
is a type that represents a N
-bit unsigned integer.
N
is the number of bits in the value. The largest size of N
is equal to
the size of the underlying type in bits.
§Examples
type Uint = BitUint<u8, 7>;
let n = Uint::new(127).unwrap();
assert_eq!(n, Uint::MAX);
assert_eq!(n.checked_add(1), None);
assert_eq!(n.get().checked_add(1), Some(128));
In this case, N
must be less than or equal to u32::BITS
:
let _ = BitUint::<u32, 33>::new(42);
N
must be greater than 0
:
let _ = BitUint::<u64, 0>::new(0);
Implementations§
Source§impl<const N: u32> BitUint<u8, N>
impl<const N: u32> BitUint<u8, N>
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this BitUint
.
The value is always 0
.
§Examples
assert_eq!(BitUint::<u8, 7>::MIN.get(), 0);
Source§impl<const N: u32> BitUint<u16, N>
impl<const N: u32> BitUint<u16, N>
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this BitUint
.
The value is always 0
.
§Examples
assert_eq!(BitUint::<u16, 7>::MIN.get(), 0);
Source§impl<const N: u32> BitUint<u32, N>
impl<const N: u32> BitUint<u32, N>
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this BitUint
.
The value is always 0
.
§Examples
assert_eq!(BitUint::<u32, 7>::MIN.get(), 0);
Source§impl<const N: u32> BitUint<u64, N>
impl<const N: u32> BitUint<u64, N>
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this BitUint
.
The value is always 0
.
§Examples
assert_eq!(BitUint::<u64, 7>::MIN.get(), 0);
Source§impl<const N: u32> BitUint<u128, N>
impl<const N: u32> BitUint<u128, N>
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this BitUint
.
The value is always 0
.
§Examples
assert_eq!(BitUint::<u128, 7>::MIN.get(), 0);
Source§impl<const N: u32> BitUint<usize, N>
impl<const N: u32> BitUint<usize, N>
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this BitUint
.
The value is always 0
.
§Examples
assert_eq!(BitUint::<usize, 7>::MIN.get(), 0);
Source§impl<const N: u32> BitUint<u8, N>
impl<const N: u32> BitUint<u8, N>
Sourcepub const fn checked_add(self, rhs: u8) -> Option<Self>
pub const fn checked_add(self, rhs: u8) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: u8) -> Option<Self>
pub const fn checked_sub(self, rhs: u8) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: u8) -> Option<Self>
pub const fn checked_mul(self, rhs: u8) -> Option<Self>
Sourcepub const fn checked_div(self, rhs: u8) -> Option<Self>
pub const fn checked_div(self, rhs: u8) -> Option<Self>
Sourcepub const fn checked_div_euclid(self, rhs: u8) -> Option<Self>
pub const fn checked_div_euclid(self, rhs: u8) -> Option<Self>
Sourcepub const fn checked_rem(self, rhs: u8) -> Option<Self>
pub const fn checked_rem(self, rhs: u8) -> Option<Self>
Sourcepub const fn checked_rem_euclid(self, rhs: u8) -> Option<Self>
pub const fn checked_rem_euclid(self, rhs: u8) -> Option<Self>
Sourcepub const fn checked_ilog(self, base: u8) -> Option<u32>
pub const fn checked_ilog(self, base: u8) -> Option<u32>
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_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Shifts self
left by rhs
bits.
Returns None
if rhs
is larger than or equal to the number of bits
in self
.
§Examples
let n = BitUint::<u8, 5>::new(0x01).unwrap();
let m = BitUint::<u8, 5>::new(0x10).unwrap();
assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u8::BITS - 1).map(BitUint::get), Some(0x00));
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, exp: u32) -> Option<Self>
pub const fn checked_pow(self, exp: u32) -> Option<Self>
Source§impl<const N: u32> BitUint<u16, N>
impl<const N: u32> BitUint<u16, N>
Sourcepub const fn checked_add(self, rhs: u16) -> Option<Self>
pub const fn checked_add(self, rhs: u16) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: u16) -> Option<Self>
pub const fn checked_sub(self, rhs: u16) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: u16) -> Option<Self>
pub const fn checked_mul(self, rhs: u16) -> Option<Self>
Sourcepub const fn checked_div(self, rhs: u16) -> Option<Self>
pub const fn checked_div(self, rhs: u16) -> Option<Self>
Sourcepub const fn checked_div_euclid(self, rhs: u16) -> Option<Self>
pub const fn checked_div_euclid(self, rhs: u16) -> Option<Self>
Sourcepub const fn checked_rem(self, rhs: u16) -> Option<Self>
pub const fn checked_rem(self, rhs: u16) -> Option<Self>
Sourcepub const fn checked_rem_euclid(self, rhs: u16) -> Option<Self>
pub const fn checked_rem_euclid(self, rhs: u16) -> Option<Self>
Sourcepub const fn checked_ilog(self, base: u16) -> Option<u32>
pub const fn checked_ilog(self, base: u16) -> Option<u32>
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_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Shifts self
left by rhs
bits.
Returns None
if rhs
is larger than or equal to the number of bits
in self
.
§Examples
let n = BitUint::<u16, 5>::new(0x01).unwrap();
let m = BitUint::<u16, 5>::new(0x10).unwrap();
assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u16::BITS - 1).map(BitUint::get), Some(0x00));
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, exp: u32) -> Option<Self>
pub const fn checked_pow(self, exp: u32) -> Option<Self>
Source§impl<const N: u32> BitUint<u32, N>
impl<const N: u32> BitUint<u32, N>
Sourcepub const fn checked_add(self, rhs: u32) -> Option<Self>
pub const fn checked_add(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: u32) -> Option<Self>
pub const fn checked_sub(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: u32) -> Option<Self>
pub const fn checked_mul(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_div(self, rhs: u32) -> Option<Self>
pub const fn checked_div(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_div_euclid(self, rhs: u32) -> Option<Self>
pub const fn checked_div_euclid(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_rem(self, rhs: u32) -> Option<Self>
pub const fn checked_rem(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_rem_euclid(self, rhs: u32) -> Option<Self>
pub const fn checked_rem_euclid(self, rhs: u32) -> Option<Self>
Sourcepub const fn checked_ilog(self, base: u32) -> Option<u32>
pub const fn checked_ilog(self, base: u32) -> Option<u32>
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_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Shifts self
left by rhs
bits.
Returns None
if rhs
is larger than or equal to the number of bits
in self
.
§Examples
let n = BitUint::<u32, 5>::new(0x01).unwrap();
let m = BitUint::<u32, 5>::new(0x10).unwrap();
assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u32::BITS - 1).map(BitUint::get), Some(0x00));
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, exp: u32) -> Option<Self>
pub const fn checked_pow(self, exp: u32) -> Option<Self>
Source§impl<const N: u32> BitUint<u64, N>
impl<const N: u32> BitUint<u64, N>
Sourcepub const fn checked_add(self, rhs: u64) -> Option<Self>
pub const fn checked_add(self, rhs: u64) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: u64) -> Option<Self>
pub const fn checked_sub(self, rhs: u64) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: u64) -> Option<Self>
pub const fn checked_mul(self, rhs: u64) -> Option<Self>
Sourcepub const fn checked_div(self, rhs: u64) -> Option<Self>
pub const fn checked_div(self, rhs: u64) -> Option<Self>
Sourcepub const fn checked_div_euclid(self, rhs: u64) -> Option<Self>
pub const fn checked_div_euclid(self, rhs: u64) -> Option<Self>
Sourcepub const fn checked_rem(self, rhs: u64) -> Option<Self>
pub const fn checked_rem(self, rhs: u64) -> Option<Self>
Sourcepub const fn checked_rem_euclid(self, rhs: u64) -> Option<Self>
pub const fn checked_rem_euclid(self, rhs: u64) -> Option<Self>
Sourcepub const fn checked_ilog(self, base: u64) -> Option<u32>
pub const fn checked_ilog(self, base: u64) -> Option<u32>
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_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Shifts self
left by rhs
bits.
Returns None
if rhs
is larger than or equal to the number of bits
in self
.
§Examples
let n = BitUint::<u64, 5>::new(0x01).unwrap();
let m = BitUint::<u64, 5>::new(0x10).unwrap();
assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u64::BITS - 1).map(BitUint::get), Some(0x00));
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, exp: u32) -> Option<Self>
pub const fn checked_pow(self, exp: u32) -> Option<Self>
Source§impl<const N: u32> BitUint<u128, N>
impl<const N: u32> BitUint<u128, N>
Sourcepub const fn checked_add(self, rhs: u128) -> Option<Self>
pub const fn checked_add(self, rhs: u128) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: u128) -> Option<Self>
pub const fn checked_sub(self, rhs: u128) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: u128) -> Option<Self>
pub const fn checked_mul(self, rhs: u128) -> Option<Self>
Sourcepub const fn checked_div(self, rhs: u128) -> Option<Self>
pub const fn checked_div(self, rhs: u128) -> Option<Self>
Sourcepub const fn checked_div_euclid(self, rhs: u128) -> Option<Self>
pub const fn checked_div_euclid(self, rhs: u128) -> Option<Self>
Sourcepub const fn checked_rem(self, rhs: u128) -> Option<Self>
pub const fn checked_rem(self, rhs: u128) -> Option<Self>
Sourcepub const fn checked_rem_euclid(self, rhs: u128) -> Option<Self>
pub const fn checked_rem_euclid(self, rhs: u128) -> Option<Self>
Sourcepub const fn checked_ilog(self, base: u128) -> Option<u32>
pub const fn checked_ilog(self, base: u128) -> Option<u32>
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_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Shifts self
left by rhs
bits.
Returns None
if rhs
is larger than or equal to the number of bits
in self
.
§Examples
let n = BitUint::<u128, 5>::new(0x01).unwrap();
let m = BitUint::<u128, 5>::new(0x10).unwrap();
assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u128::BITS - 1).map(BitUint::get), Some(0x00));
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, exp: u32) -> Option<Self>
pub const fn checked_pow(self, exp: u32) -> Option<Self>
Source§impl<const N: u32> BitUint<usize, N>
impl<const N: u32> BitUint<usize, N>
Sourcepub const fn checked_add(self, rhs: usize) -> Option<Self>
pub const fn checked_add(self, rhs: usize) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: usize) -> Option<Self>
pub const fn checked_sub(self, rhs: usize) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: usize) -> Option<Self>
pub const fn checked_mul(self, rhs: usize) -> Option<Self>
Sourcepub const fn checked_div(self, rhs: usize) -> Option<Self>
pub const fn checked_div(self, rhs: usize) -> Option<Self>
Sourcepub const fn checked_div_euclid(self, rhs: usize) -> Option<Self>
pub const fn checked_div_euclid(self, rhs: usize) -> Option<Self>
Sourcepub const fn checked_rem(self, rhs: usize) -> Option<Self>
pub const fn checked_rem(self, rhs: usize) -> Option<Self>
Sourcepub const fn checked_rem_euclid(self, rhs: usize) -> Option<Self>
pub const fn checked_rem_euclid(self, rhs: usize) -> Option<Self>
Sourcepub const fn checked_ilog(self, base: usize) -> Option<u32>
pub const fn checked_ilog(self, base: usize) -> Option<u32>
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_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Sourcepub const fn checked_shl(self, rhs: u32) -> Option<Self>
pub const fn checked_shl(self, rhs: u32) -> Option<Self>
Shifts self
left by rhs
bits.
Returns None
if rhs
is larger than or equal to the number of bits
in self
.
§Examples
let n = BitUint::<usize, 5>::new(0x01).unwrap();
let m = BitUint::<usize, 5>::new(0x10).unwrap();
assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(usize::BITS - 1).map(BitUint::get), Some(0x00));
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, exp: u32) -> Option<Self>
pub const fn checked_pow(self, exp: u32) -> Option<Self>
Source§impl<T: Unsigned + PrimInt, const N: u32> BitUint<T, N>
impl<T: Unsigned + PrimInt, const N: u32> BitUint<T, N>
Sourcepub const unsafe fn new_unchecked(n: T) -> Self
pub const unsafe fn new_unchecked(n: T) -> Self
Creates a new BitUint
with the given unsigned integer value.
This method does not check whether the value is a valid N
-bit unsigned
integer. This results in undefined behaviour if the value is not a valid
N
-bit unsigned integer.
§Safety
The value must be a valid N
-bit unsigned integer.