Int
A library to convert unsigned integers to signed integers using two's complement. It contains basic arithmetic operations for signed integers.
Uses arithmetic shr and shl for negative numbers
Structs
Int
struct Int has copy, drop, store {
value: u256
}value - The number.
const EQUAL: u8 = 0;
const LESS_THAN: u8 = 1;
const GREATER_THAN: u8 = 2;Interface
value
It returns the inner value inside self.
@param self: The Int struct.
@return u256.
zero
It creates a zero Int.
@return Int. The wrapped value.
one
It creates a one Int.
@return Int. The wrapped value.
max
It creates the largest possible Int.
Maximum number is : 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
@return Int.
from_u8
It wraps a u8 value into an Int.
@param value: The u8 value to wrap
@return Int. The wrapped
value
from_u16
It wraps a u16 value into an Int.
@param value: The u16 value to wrap
@return Int. The wrapped
value
from_u32
It wraps a u32 value into an Int.
@param value: The u32 value to wrap
@return Int. The wrapped
value
from_u64
It wraps a u64 value into an Int.
@param value: The u64 value to wrap
@return Int. The wrapped
value
from_u128
It wraps a u128 value into an Int.
@param value: The u128 value to wrap
@return Int. The wrapped
value
from_u256
It wraps a u128 value into an Int.
@param value: The u256 value to wrap
@return Int. The wrapped
value
Abort
if value is larger than 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.
neg_from_u8
It wraps a u8 value into an Int and negates it.
@param value: The u16 value to wrap
@return Int. The wrapped
value
neg_from_u16
It wraps a u16 value into an Int and negates it.
@param value: The u16 value to wrap
@return Int. The wrapped
value
neg_from_u32
It wraps a u32 value into an Int and negates it.
@param value: The u32 value to wrap
@return Int. The wrapped
value
neg_from_u64
It wraps a u64 value into an Int and negates it.
@param value: The u64 value to wrap
@return Int. The wrapped
value
neg_from_u128
It wraps a u128 value into an Int and negates it.
@param value: The u128 value to wrap
@return Int. The wrapped
value
neg_from_u256
It wraps a u256 value into an Int and negates it.
@param value: The u256 value to wrap
@return Int. The wrapped
value
to_u8
It unwraps the value inside self and casts it to u8.
@param self: The Int struct.
@return u8. The inner value cast to u8.
Abort
self.valueis negative
to_u16
It unwraps the value inside self and casts it to u16.
@param self: The Int struct.
@return u16. The inner value cast to u16.
Abort
self.valueis negative
to_u32
It unwraps the value inside self and casts it to u32.
@param self: The Int struct.
@return u32. The inner value cast to u32.
Abort
self.valueis negative
to_u64
It unwraps the value inside self and casts it to u64.
@param self: The Int struct.
@return u64. The inner value cast to u64.
Abort
self.valueis negative
to_u128
It unwraps the value inside self and casts it to u128.
@param self: The Int struct.
@return u128. The inner value cast to u128.
Abort
self.valueis negative
to_u256
It unwraps the value inside self and casts it to u256.
@param self: The Int struct.
@return u128. The inner value cast to u256.
Abort
self.valueis negative
truncate_to_u8
It unwraps the value inside self and truncates it to u8.
@param self: The Int struct.
@return u8. The inner value is truncated to u8.
truncate_to_u16
It unwraps the value inside self and truncates it to u16.
@param self: The Int struct.
@return u8. The inner value is truncated to u16.
truncate_to_u32
It unwraps the value inside self and truncates it to u16.
@param self: The Int struct.
@return u8. The inner value is truncated to u32.
truncate_to_u64
It unwraps the value inside self and truncates it to u64.
@param self: The Int struct.
@return u8. The inner value is truncated to u64.
truncate_to_u128
It unwraps the value inside self and truncates it to u128.
@param self: The Int struct.
@return u8. The inner value is truncated to u128.
flip
It flips the sign of self.
@param self: The Int struct.
@return Int. The returned Int will have its signed flipped.
abs
It returns the absolute of an Int.
@param self: The Int struct.
@return Int. The absolute.
is_neg
It checks if self is negative.
@param self: The Int struct.
@return bool.
is_zero
It checks if self is zero.
@param self: The Int struct.
@return bool.
is_positive
It checks if self is positive.
@param self: The Int struct.
@return bool.
compare
It compares a and b.
@param a: An Int struct.
@param b: An Int struct.
@return 0. a == b.
@return 1. a < b.
@return 2. a > b.
eq
It checks if a and b are equal.
@param a: An Int struct.
@param b: An Int struct.
@return bool.
lt
It checks if a < b.
@param a: An Int struct.
@param b: An Int struct.
@return bool.
lte
It checks if a <= b.
@param a: An Int struct.
@param b: An Int struct.
@return bool.
gt
It checks if a > b.
@param a: An Int struct.
@param b: An Int struct.
@return bool.
gte
It checks if a >= b.
@param a: An Int struct.
@param b: An Int struct.
@return bool.
add
It checks if a >= b.
@param a: An Int struct.
@param b: An Int struct.
@return Int. The result of
a+b.
sub
It performs a - b.
@param a: An Int struct.
@param b: An Int struct.
@return Int. The result of
a-b.
mul
It performs a * b.
@param a: An Int struct.
@param b: An Int struct.
@return Int. The result of
a*b.
div_down
It performs a / b rounding down.
@param a: An Int struct.
@param b: An Int struct.
@return Int. The result of
a/brounding down.
div_up
It performs a / b rounding up.
@param a: An Int struct.
@param b: An Int struct.
@return Int. The result of
a/brounding up.
mod
It performs a % b.
@param a: An Int struct.
@param b: An Int struct.
@return Int. The result of
a%b.
pow
It performs base ** exponent.
@param base: An Int struct.
@param exponent: The exponent.
@return Int. The result of
base**exponent.
shr
It performs self >> rhs.
@param self: An Int struct.
@param rhs: The value to right-hand shift.
@return Int. The result of
self>>rhs.
shl
It performs self << lhs.
@param self: An Int struct.
@param lhs: The value to right-hand shift.
@return Int. The result of
self<<lhs.
or
It performs a | b.
@param a: The first operand.
@param b: The second operand.
@return Int. The result of
a|b.
and
It performs a & b.
@param a: The first operand.
@param b: The second operand.
@return Int. The result of
a&b.
Last updated
Was this helpful?