Struct Subspace :: sus :: num :: u16
A 16-bit unsigned integer.
See the namespace level documentation for more.
Static Data Members
Static Methods
Construction from unsigned enum types where no bits are lost.
For conversions from types with a larger range use
try_from
. For lossy
conversions use the Cast
concept
with sus::cast<u16>(x)
.
Construction from unsigned primitive types where no bits are lost.
For conversions from types with a larger range use
try_from
. For lossy
conversions use the Cast
concept
with sus::cast<u16>(x)
.
Construction from unsigned types where no bits are lost.
For conversions from types with a larger range use
try_from
. For lossy conversions
use the Cast
concept with
sus::cast<u16>(x)
.
Default constructor, which sets the integer to 0.
Satisfies the Default
concept.
Construction from unsigned enum class types where no bits are lost.
For conversions from types with a larger range use
try_from
. For lossy
conversions use the
Cast
concept with
sus::cast<u16>(x)
.
Constructs a u16
from an unsigned primitive integer type (unsigned int
, unsigned long
, etc.) where no bits are lost.
Constructs a u16
from an unsigned enum
type (or enum class
)
where no bits are lost.
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.
Create an integer value from its representation as a byte array in big endian.
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.
Create an integer value from its representation as a byte array in little endian.
Create an integer value from its memory representation as a byte array in native endianness.
As the target platform's native endianness is used, portable code likely
wants to use from_be_bytes()
or from_le_bytes()
, as appropriate instead.
Constructs a u16
from an Iterator
by computing the product of all
elements in the iterator.
This method should rarely be called directly, as it is used to satisfy the
Product
concept so that
Iterator::product
can be
called for iterators over u16.
To handle overflow without panicing, instead of iter.product()
, use
iter.product<OverflowInteger<u16>>()
.
Panics
This method will panic if the product of all values overflows and overflow checks are enabled (they are by default) and it will wrap if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
Constructs a u16
from an Iterator
by computing the sum of all
elements in the itertor.
This method should rarely be called directly, as it is used to satisfy the
Sum
concept so that
Iterator::sum
can be called for
iterators over u16.
To handle overflow without panicing, instead of iter.sum()
, use
iter.sum<OverflowInteger<u16>>()
.
Panics
This method will panic if the sum of all values overflows if overflow checks are enabled (they are by default) and will wrap if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
sus::num::Unsigned<U>
Tries to construct a u16
from an unsigned integer type (u8
, u16
, u32
, etc.).
Returns an error if the source value is outside of the range of u16
.
sus::num::UnsignedPrimitiveInteger<U>
Tries to construct a u16
from an unsigned primitive integer type
(unsigned int
, unsigned long
, etc).
Returns an error if the source value is outside of the range of u16
.
sus::num::Signed<S>
Tries to construct a u16
from a signed integer type (i8
, i16
, i32
, etc.).
Returns an error if the source value is outside of the range of u16
.
sus::num::SignedPrimitiveInteger<S>
Tries to construct a u16
from a signed primitive integer type (
int
, long
, etc.).
Returns an error if the source value is outside of the range of u16
.
SignedPrimitiveEnum<S> || SignedPrimitiveEnumClass<S>
Tries to construct a u16
from a signed enum
type (or
enum class
).
Returns an error if the source value is outside of the range of u16
.
UnsignedPrimitiveEnum<U> || UnsignedPrimitiveEnumClass<U>
Tries to construct a u16
from an unsigned enum
type (or
enum class
).
Returns an error if the source value is outside of the range of u16
.
Methods
Computes the absolute difference between self
and other
.
Returns a mutable pointer to the underlying C++ primitive value type.
This allows Subspace numerics be used with APIs that expect a pointer to a C++ primitive type.
Returns a const pointer to the underlying C++ primitive value type.
This allows Subspace numerics be used with APIs that expect a pointer to a C++ primitive type.
Checked integer addition. Computes self + rhs
, returning None
if
overflow occurred.
Checked integer addition with a signed rhs
. Computes self + rhs
,
returning None
if overflow occurred.
Checked integer division. Computes self / rhs
, returning None
if
rhs == 0
.
Checked Euclidean division. Computes self.
div_euclid
(rhs)
, returning None
if rhs == 0
.
Returns the logarithm of the number with respect to an arbitrary base, rounded down.
Returns None if the number is zero, or if the base is not at least 2.
This method might not be optimized owing to implementation details;
checked_log2
can produce results
more efficiently for base 2, and
checked_log10
can produce results
more efficiently for base 10.
Returns the base 10 logarithm of the number, rounded down.
Returns None if the number is zero.
Returns the base 2 logarithm of the number, rounded down.
Returns None
if the number is zero.
Checked integer multiplication. Computes self * rhs
, returning None
if
overflow occurred.
Checked negation. Computes -self
, returning None unless self == 0
.
Note that negating any positive integer will overflow.
Calculates the smallest value greater than or equal to itself that is a
multiple of rhs
. Returns None
if rhs
is zero or the operation would
result in overflow.
Examples
Basic usage:
sus_check((16_u32).checked_next_multiple_of(8u) == sus::some(16u));
sus_check((23_u32).checked_next_multiple_of(8u) == sus::some(24u));
sus_check((1_u32).checked_next_multiple_of(0u) == sus::none());
sus_check(u32::MAX.checked_next_multiple_of(2u) == sus::none());
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.
Checked exponentiation. Computes pow
,
returning None
if overflow occurred.
Checked integer remainder. Computes self % rhs
, returning None
if
rhs == 0
.
Checked Euclidean modulo. Computes self.
rem_euclid
(rhs)
, returning None
if
rhs == 0
.
Checked shift left. Computes self << rhs
, returning None
if rhs
is
larger than or equal to the number of bits in self
.
Checked shift right. Computes self >> rhs
, returning None
if rhs
is
larger than or equal to the number of bits in self
.
Checked integer subtraction. Computes self - rhs
, returning None
if
overflow occurred.
Returns the number of ones in the binary representation of the current value.
Returns the number of zeros in the binary representation of the current value.
Calculates the quotient of itself and rhs
, rounding the result towards
positive infinity.
Panics
This function will panic if rhs
is 0.
Examples
Basic usage:
sus_check((7_u8).div_ceil(4u) == 2u);
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 0.
Returns true
if and only if self == 2^k
for some k
.
Returns the number of leading ones in the binary representation of the current value.
Returns the number of leading zeros in the binary representation of the current value.
Returns the logarithm of the number with respect to an arbitrary base, rounded down.
This method might not be optimized owing to implementation details;
log2
can produce results more efficiently
for base 2, and log10
can produce results
more efficiently for base 10.
Panics
This function will panic if the current value is zero, or if base
is less
than 2.
Returns the base 10 logarithm of the number, rounded down.
Panics
When the number is zero the function will panic.
Returns the base 2 logarithm of the number, rounded down.
Panics
When the number is zero the function will panic.
Calculates the smallest value greater than or equal to itself 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 (they are by default) and wrap if overflow checks are disabled (not the default).
Examples
Basic usage:
sus_check((16_u32).next_multiple_of(8u) == 16u);
sus_check((23_u32).next_multiple_of(8u) == 24u);
Returns the smallest power of two greater than or equal to self.
Panics
The function panics when the return value overflows (i.e.,
self > (1u << (u16::BITS - 1u))
) if overflow checks
are enabled (they are by default) and will wrap if overflow checks are
disabled (not the default).
See overflow checks for controlling this behaviour.
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.
Calculates self + rhs
with a signed 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.
Calculates the divisor when self
is divided by rhs
.
Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would occur. Note that for unsigned integers overflow never occurs, so the second value is always false.
Panics
This function will panic if rhs
is 0.
Calculates the quotient of Euclidean division self.
div_euclid
(rhs)
.
Returns a tuple of the divisor along with a boolean indicating whether an
arithmetic overflow would occur. Note that for unsigned integers overflow
never occurs, so the second value is always false. Since, for the positive
integers, all common definitions of division are equal, this is exactly
equal to self.overflowing_div(rhs)
.
Panics
This function will panic if rhs
is 0.
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.
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.
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.
Calculates the remainder when self
is divided by rhs
.
Returns a tuple of the remainder after dividing along with a boolean indicating whether an arithmetic overflow would occur. Note that for unsigned integers overflow never occurs, so the second value is always false.
Panics
This function will panic if rhs
is 0.
Calculates the remainder self.
rem_euclid
(rhs)
as if by Euclidean division.
Returns a tuple of the modulo after dividing along with a boolean indicating
whether an arithmetic overflow would occur. Note that for unsigned integers
overflow never occurs, so the second value is always false. Since, for the
positive integers, all common definitions of division are equal, this
operation is exactly equal to self.overflowing_rem(rhs)
.
Panics
This function will panic if rhs
is 0.
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 by (N-1)
where
N
is the number of bits, and this value is then used to perform the shift.
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 by (N-1)
where N
is the number of bits, and this value is then used to perform the
shift.
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.
Raises self to the power of exp
, using exponentiation by squaring.
Panics
This function will panic on overflow if overflow checks are enabled (they are by default) and will wrap if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
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 0.
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.
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!
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!
Saturating integer addition. Computes self + rhs
, saturating at the
numeric bounds instead of overflowing.
Saturating integer addition with a signed rhs
. Computes self + rhs
,
saturating at the numeric bounds instead of overflowing.
Saturating integer division. Computes self / rhs
, saturating at the
numeric bounds instead of overflowing. Note that for unsigned integers
overflow never occurs, so the value will always be strictly the result of
division.
Panics
This function will panic if rhs
is 0.
Saturating integer multiplication. Computes self * rhs
, saturating at the
numeric bounds instead of overflowing.
Saturating integer subtraction. Computes self - rhs
, saturating at the
numeric bounds instead of overflowing.
Reverses the byte order of the integer.
Converts self to big endian from the target's endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
Return the memory representation of this integer as a byte array in big-endian (network) byte order.
Converts self to little endian from the target's endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
Return the memory representation of this integer as a byte array in little-endian byte order.
Return the memory representation of this integer as a byte array in native byte order.
As the target platform's native endianness is used, portable code should use
to_be_bytes()
or to_le_bytes()
, as appropriate, instead.
Returns the number of trailing ones in the binary representation of the current value.
Returns the number of trailing zeros in the binary representation of the current value.
Unchecked integer addition. Computes self + rhs
, assuming overflow cannot
occur.
Safety
This function is allowed to result in undefined behavior when
self + rhs >
MAX
or
self + rhs <
MIN
, i.e. when
checked_add
would return None
.
Unchecked integer multiplication. Computes self * rhs
, assuming overflow
cannot occur.
Safety
This function is allowed to result in undefined behavior when
self * rhs >
MAX
or
self * rhs <
MIN
, i.e. when
checked_mul
would return None
.
Unchecked integer subtraction. Computes self - rhs
, assuming overflow
cannot occur.
Safety
This function is allowed to result in undefined behavior when
self - rhs >
MAX
or
self - rhs <
MIN
, i.e. when
checked_sub
would return None
.
Wrapping (modular) addition. Computes self + rhs
, wrapping around at the
boundary of the type.
Wrapping (modular) addition with an unsigned rhs. Computes self + rhs
,
wrapping around at the boundary of the type.
Wrapping (modular) division. Computes self / rhs
. Wrapped division on
unsigned types is just normal division. There's no way wrapping could ever
happen. This function exists, so that all operations are accounted for in
the wrapping operations.
Panics
This function will panic if rhs
is 0.
Wrapping Euclidean division. Computes self.
div_euclid
(rhs)
. Wrapped division
on unsigned types is just normal division.
There's no way wrapping could ever happen. This function exists so that all
operations are accounted for in the wrapping operations. Since, for the
positive integers, all common definitions of division are equal, this is
exactly equal to self.wrapping_div(rhs)
.
Panics
This function will panic if rhs
is 0.
Wrapping (modular) multiplication. Computes self * rhs
, wrapping around
at the boundary of the type.
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). For values smaller than the
corresponding signed type's maximum the result is the same as casting
the corresponding signed value. Any larger values are equivalent to
MAX + 1 - (val - MAX - 1)
where MAX is the corresponding signed type's
maximum.
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.
Wrapping (modular) exponentiation. Computes pow
, wrapping around at the boundary of the type.
Wrapping (modular) remainder. Computes self % rhs
. Wrapped remainder
calculation on unsigned types is just the regular remainder calculation.
There's no way wrapping could ever happen. This function exists, so that all operations are accounted for in the wrapping operations.
Panics
This function will panic if rhs
is 0.
Wrapping Euclidean modulo. Computes self.
rem_euclid
(rhs)
. Wrapped modulo
calculation on unsigned types is just the regular remainder calculation.
There’s no way wrapping could ever happen. This function exists, so that all
operations are accounted for in the wrapping operations. Since, for the
positive integers, all common definitions of division are equal, this is
exactly equal to self.wrapping_rem(rhs)
.
Panics
This function will panic if rhs
is 0.
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 Subspace
integer types all implement a rotate_left
function, which may be what you
want instead.
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 Subspace
integer types all implement a rotate_right
function, which may be what you
want instead.
Wrapping (modular) subtraction. Computes self - rhs
, wrapping around at
the boundary of the type.
Conversions
sus::num::UnsignedPrimitiveInteger<U>
::sus::mem::size_of<U>() >= ::sus::mem::size_of<_primitive>()
Conversion from the numeric type to a C++ primitive type.
This converts to unsigned primitives which are at least as large as the
u16
.
auto d = uint32_t{3_u32}; // Compiles.
auto e = uint32_t(3_u32); // Compiles.
uint32_t f = 3_u32; // Compiles.
auto d = uint16_t{3_u32}; // Does not compile.
auto e = uint16_t(3_u32); // Does not compile.
uint16_t f = 3_u32; // Does not compile.
Potentially-lossy type conversions can be forced through the
Cast
concept, such as
sus::cast<uint16_t>(3_u32)
or sus::cast<int32_t>(3_u32)
, or even
sus::cast<i32>(3_u32)
.
Operators
Assigns the remainder of dividing itself by r
.
Satisfies the RemAssign<u16>
concept.
Panics
This operation will panic if r
is 0.
Assigns the bitwise AND of itself and r
.
Satisfies the BitAndAssign<u16>
concept.
Multiplies itself by r
.
Satisfies the MulAssign<u16>
concept.
Panics
This operation will panic on overflow if overflow checks are enabled (they are by default) and will wrap if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
Adds r
to itself.
Satisfies the AddAssign<u16>
concept.
Panics
This operation will panic on overflow if overflow checks are enabled (they are by default) and will wrap if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
Subtracts r
from itself.
Satisfies the SubAssign<u16>
concept.
Panics
This operation will panic on overflow if overflow checks are enabled (they are by default) and will wrap if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
Divides itself by r
.
Satisfies the DivAssign<u16>
concept.
Panics
This operation will panic if r
is 0.
Shifts itself left by r
bits.
Panics
This function will panic when r
is not less than the number of bits in
u16
if overflow checks are enabled (they are by default) and will
perform a wrapping shift if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
Satisfies the ShlAssign<u16>
concept.
Assignment from unsigned types (u8
, u16
, etc.) where no bits are lost.
For conversions from types with a larger range use
try_from
. For lossy conversions
use the Cast
concept with
sus::cast<u16>(x)
.
Assignment from unsigned primitive types (unsigned int
, unsigned long
,
etc.) where no bits are lost.
For conversions from types with a larger range use
try_from
. For lossy
conversions use the
Cast
concept with
sus::cast<u16>(x)
.
Assignment from unsigned enum types where no bits are lost.
For conversions from types with a larger range use
try_from
. For lossy
conversions use the
Cast
concept with
sus::cast<u16>(x)
.
Shifts itself right by r
bits.
Satisfies the ShrAssign<u16>
concept.
Panics
This function will panic when r
is not less than the number of bits in
u16
if overflow checks are enabled (they are by default) and will
perform a wrapping shift if overflow checks are disabled (not the default).
See overflow checks for controlling this behaviour.
Assigns the bitwise XOR of itself and r
.
Satisfies the BitXorAssign<u16>
concept.
Assigns the bitwise OR of itself and r
.
Satisfies the BitOrAssign<u16>
concept.
Computes the bitwise complement (bitwise inverse) of the current value.
Satisfies the BitNot<u16>
concept.
Data Members
The inner primitive value. Prefer to cast to the desired primitive type,
such as with uint32_t{n}
for a numeric value n
.