Mojo struct
UInt
@register_passable(trivial)
struct UInt
This type represents an unsigned integer.
The size of this unsigned integer is platform-dependent.
If you wish to use a fixed size unsigned integer, consider using
UInt8
, UInt16
, UInt32
, or UInt64
.
Implemented traits
Absable
,
AnyType
,
Boolable
,
CeilDivable
,
Copyable
,
Defaultable
,
DivModable
,
EqualityComparable
,
ExplicitlyCopyable
,
GreaterThanComparable
,
GreaterThanOrEqualComparable
,
Hashable
,
Indexer
,
Intable
,
LessThanComparable
,
LessThanOrEqualComparable
,
Movable
,
Representable
,
Stringable
,
UnknownDestructibility
,
Writable
Aliases
__copyinit__is_trivial
alias __copyinit__is_trivial = True
__del__is_trivial
alias __del__is_trivial = True
__moveinit__is_trivial
alias __moveinit__is_trivial = True
BITWIDTH
alias BITWIDTH = Int(bit_width_of[::DType,__mlir_type.!kgen.target]())
The bit width of the integer type.
MAX
alias MAX = UInt((0 if (Int(bit_width_of[::DType,__mlir_type.!kgen.target]()) < 0) else (1 << Int(bit_width_of[::DType,__mlir_type.!kgen.target]())) + -1))
Returns the maximum integer value.
MIN
alias MIN = UInt(0)
Returns the minimum value of type.
Methods
__init__
__init__() -> Self
Default constructor that produces zero.
@implicit
__init__(value: IntLiteral[value]) -> Self
Construct UInt from the given IntLiteral value.
Args:
- value (
IntLiteral
): The init value.
@implicit
__init__(value: Int) -> Self
Construct UInt from the given Int value.
Args:
- value (
Int
): The init value.
__init__[T: Indexer](value: T) -> Self
Construct UInt from the given Indexable value.
Parameters:
- T (
Indexer
): The type that that can index into a collection or pointer.
Args:
- value (
T
): The init value.
__bool__
__bool__(self) -> Bool
Convert this Int to Bool.
Returns:
Bool
: False Bool value if the value is equal to 0 and True otherwise.
__pos__
__pos__(self) -> Self
Return +self.
Returns:
Self
: The +self value.
__invert__
__invert__(self) -> Self
Return ~self.
Returns:
Self
: The ~self value.
__lt__
__lt__(self, rhs: Self) -> Bool
Return whether this UInt is strictly less than another.
Args:
- rhs (
Self
): The other UInt to compare against.
Returns:
Bool
: True if this UInt is less than the other UInt and False otherwise.
__le__
__le__(self, rhs: Self) -> Bool
Compare this Int to the RHS using LE comparison.
Args:
- rhs (
Self
): The other UInt to compare against.
Returns:
Bool
: True if this Int is less-or-equal than the RHS Int and False
otherwise.
__eq__
__eq__(self, rhs: Self) -> Bool
Compare this UInt to the RHS using EQ comparison.
Args:
- rhs (
Self
): The other UInt to compare against.
Returns:
Bool
: True if this UInt is equal to the RHS UInt and False otherwise.
__ne__
__ne__(self, rhs: Self) -> Bool
Compare this UInt to the RHS using NE comparison.
Args:
- rhs (
Self
): The other UInt to compare against.
Returns:
Bool
: True if this UInt is non-equal to the RHS UInt and False otherwise.
__gt__
__gt__(self, rhs: Self) -> Bool
Return whether this UInt is strictly greater than another.
Args:
- rhs (
Self
): The other UInt to compare against.
Returns:
Bool
: True if this UInt is greater than the other UInt and False
otherwise.
__ge__
__ge__(self, rhs: Self) -> Bool
Return whether this UInt is greater than or equal to another.
Args:
- rhs (
Self
): The other UInt to compare against.
Returns:
Bool
: True if this UInt is greater than or equal to the other UInt and
False otherwise.
__add__
__add__(self, rhs: Self) -> Self
Return self + rhs
.
Args:
- rhs (
Self
): The value to add.
Returns:
Self
: self + rhs
value.
__sub__
__sub__(self, rhs: Self) -> Self
Return self - rhs
.
Args:
- rhs (
Self
): The value to subtract.
Returns:
Self
: self - rhs
value.
__mul__
__mul__(self, rhs: Self) -> Self
Return self * rhs
.
Args:
- rhs (
Self
): The value to multiply with.
Returns:
Self
: self * rhs
value.
__truediv__
__truediv__(self, rhs: Self) -> SIMD[float64, 1]
Return the floating point division of self
and rhs
.
Args:
- rhs (
Self
): The value to divide on.
Returns:
SIMD
: Float64(self)/Float64(rhs)
value.
__floordiv__
__floordiv__(self, rhs: Self) -> Self
Return the division of self
and rhs
rounded down to the nearest integer.
Args:
- rhs (
Self
): The value to divide on.
Returns:
Self
: floor(self/rhs)
value.
__mod__
__mod__(self, rhs: Self) -> Self
Return the remainder of self divided by rhs.
Args:
- rhs (
Self
): The value to divide on.
Returns:
Self
: The remainder of dividing self by rhs.
__pow__
__pow__(self, exp: Self) -> Self
Return the value raised to the power of the given exponent.
Computes the power of an integer using the Russian Peasant Method.
Args:
- exp (
Self
): The exponent value.
Returns:
Self
: The value of self
raised to the power of exp
.
__lshift__
__lshift__(self, rhs: Self) -> Self
Return self << rhs
.
Args:
- rhs (
Self
): The value to shift with.
Returns:
Self
: self << rhs
.
__rshift__
__rshift__(self, rhs: Self) -> Self
Return self >> rhs
.
Args:
- rhs (
Self
): The value to shift with.
Returns:
Self
: self >> rhs
.
__and__
__and__(self, rhs: Self) -> Self
Return self & rhs
.
Args:
- rhs (
Self
): The RHS value.
Returns:
Self
: self & rhs
.
__or__
__or__(self, rhs: Self) -> Self
Return self | rhs
.
Args:
- rhs (
Self
): The RHS value.
Returns:
Self
: self | rhs
.
__xor__
__xor__(self, rhs: Self) -> Self
Return self ^ rhs
.
Args:
- rhs (
Self
): The RHS value.
Returns:
Self
: self ^ rhs
.
__radd__
__radd__(self, value: Self) -> Self
Return value + self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value + self
.
__rsub__
__rsub__(self, value: Self) -> Self
Return value - self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value - self
.
__rmul__
__rmul__(self, value: Self) -> Self
Return value * self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value * self
.
__rfloordiv__
__rfloordiv__(self, value: Self) -> Self
Return value // self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value // self
.
__rmod__
__rmod__(self, value: Self) -> Self
Return value % self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value % self
.
__rpow__
__rpow__(self, value: Self) -> Self
Return pow(value,self)
.
Args:
- value (
Self
): The other value.
Returns:
Self
: pow(value,self)
.
__rlshift__
__rlshift__(self, value: Self) -> Self
Return value << self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value << self
.
__rrshift__
__rrshift__(self, value: Self) -> Self
Return value >> self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value >> self
.
__rand__
__rand__(self, value: Self) -> Self
Return value & self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value & self
.
__ror__
__ror__(self, value: Self) -> Self
Return value | self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value | self
.
__rxor__
__rxor__(self, value: Self) -> Self
Return value ^ self
.
Args:
- value (
Self
): The other value.
Returns:
Self
: value ^ self
.
__iadd__
__iadd__(mut self, rhs: Self)
Compute self + rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__isub__
__isub__(mut self, rhs: Self)
Compute self - rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__imul__
__imul__(mut self, rhs: Self)
Compute self*rhs and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__itruediv__
__itruediv__(mut self, rhs: Self)
Compute self / rhs
, convert to int, and save the result in self.
Since floor(self / rhs)
is equivalent to self // rhs
, this yields
the same as __ifloordiv__
.
Args:
- rhs (
Self
): The RHS value.
__ifloordiv__
__ifloordiv__(mut self, rhs: Self)
Compute self // rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__imod__
__imod__(mut self, rhs: Self)
Compute self % rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ipow__
__ipow__(mut self, rhs: Self)
Compute pow(self, rhs)
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ilshift__
__ilshift__(mut self, rhs: Self)
Compute self << rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__irshift__
__irshift__(mut self, rhs: Self)
Compute self >> rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__iand__
__iand__(mut self, rhs: Self)
Compute self & rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ixor__
__ixor__(mut self, rhs: Self)
Compute self ^ rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ior__
__ior__(mut self, rhs: Self)
Compute self|rhs and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__divmod__
__divmod__(self, rhs: Self) -> Tuple[UInt, UInt]
Computes both the quotient and remainder using integer division.
Args:
- rhs (
Self
): The value to divide on.
Returns:
Tuple
: The quotient and remainder as a Tuple(self // rhs, self % rhs)
.
__index__
__index__(self) -> index
Convert to index.
Returns:
index
: The corresponding __mlir_type.index value.
__int__
__int__(self) -> Int
Gets the integral value, wrapping to a negative number on overflow.
Returns:
Int
: The value as an integer.
__abs__
__abs__(self) -> Self
Return the absolute value of the UInt value.
Returns:
Self
: The absolute value.
__ceil__
__ceil__(self) -> Self
Return the ceiling of the UInt value, which is itself.
Returns:
Self
: The UInt value itself.
__floor__
__floor__(self) -> Self
Return the floor of the UInt value, which is itself.
Returns:
Self
: The UInt value itself.
__round__
__round__(self) -> Self
Return the rounded value of the UInt value, which is itself.
Returns:
Self
: The UInt value itself.
__round__(self, ndigits: Self) -> Self
Return the rounded value of the UInt value, which is itself.
Args:
- ndigits (
Self
): The number of digits to round to.
Returns:
Self
: The UInt value itself if ndigits >= 0 else the rounded value.
__trunc__
__trunc__(self) -> Self
Return the truncated UInt value, which is itself.
Returns:
Self
: The Int value itself.
__ceildiv__
__ceildiv__(self, denominator: Self) -> Self
Return the rounded-up result of dividing self by denominator.
Args:
- denominator (
Self
): The denominator.
Returns:
Self
: The ceiling of dividing numerator by denominator.
is_power_of_two
is_power_of_two(self) -> Bool
Check if the integer is a (non-zero) power of two.
Returns:
Bool
: True if the integer is a power of two, False otherwise.
write_to
write_to(self, mut writer: T)
Formats this integer to the provided Writer.
Args:
- writer (
T
): The object to write to.
__str__
__str__(self) -> String
Convert this UInt to a string.
A small example.
x = UInt(50)
assert_equal(String(x), "50")
Returns:
String
: The string representation of this UInt.
__repr__
__repr__(self) -> String
Convert this UInt to a string.
A small example.
x = UInt(50)
assert_equal(repr(x), "UInt(50)")
Returns:
String
: The string representation of this UInt.
__hash__
__hash__[H: Hasher](self, mut hasher: H)
Updates hasher with this uint value.
Parameters:
- H (
Hasher
): The hasher type.
Args:
- hasher (
H
): The hasher instance.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!