# Int

A library to convert unsigned integers to signed integers using two's complement. It contains basic arithmetic operations for signed integers.

{% hint style="info" %}
Uses arithmetic shr and shl for negative numbers
{% endhint %}

## Structs

### <mark style="color:blue;">**Int**</mark>

```rust
struct Int has copy, drop, store {
    value: u256
 }
```

* **value** - The number.

```rust
const EQUAL: u8 = 0;

const LESS_THAN: u8 = 1;

const GREATER_THAN: u8 = 2;
```

## Interface

### <mark style="color:blue;">value</mark>

**It returns the inner value inside `self`.**

```rust
public fun value(self: Int): u256
```

* **@param self:** The Int struct.
* **@return u256**.

### <mark style="color:blue;">zero</mark>

**It creates a zero `Int`.**

```rust
public fun zero(): Int
```

* **@return Int.** The wrapped value.

### <mark style="color:blue;">one</mark>

**It creates a one `Int`.**

```rust
public fun one(): Int
```

* **@return Int.** The wrapped value.

### <mark style="color:blue;">max</mark>

**It creates the largest possible `Int`.**

{% hint style="info" %}
Maximum number is : 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
{% endhint %}

```rust
public fun max(): Int
```

* **@return Int.**&#x20;

### <mark style="color:blue;">from\_u8</mark>

**It wraps a u8 `value` into an `Int`.**

```rust
public fun from_u8(value: u8): Int
```

* **@param value:** The u8 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">from\_u16</mark>

**It wraps a u16 `value` into an `Int`.**

```rust
public fun from_u16(value: u16): Int
```

* **@param value:** The u16 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">from\_u32</mark>

**It wraps a u32 `value` into an `Int`.**

```rust
public fun from_u32(value: u32): Int
```

* **@param value:** The u32 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">from\_u64</mark>

**It wraps a u64 `value` into an `Int`.**

```rust
public fun from_u64(value: u64): Int
```

* **@param value:** The u64 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">from\_u128</mark>

**It wraps a u128 `value` into an `Int`.**

```rust
public fun from_u128(value: u128): Int
```

* **@param value:** The u128 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">from\_u256</mark>

**It wraps a u128 `value` into an `Int`.**

```rust
public fun from_u256(value: u256): Int
```

* **@param value:** The u256 value to wrap
* **@return Int.** The wrapped `value`

**`Abort`**

* if value is larger than 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.

### <mark style="color:blue;">neg\_from\_u8</mark>

**It wraps a u8 `value` into an `Int` and negates it.**

```rust
public fun neg_from_u8(value: u8): Int
```

* **@param value:** The u16 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">neg\_from\_u16</mark>

**It wraps a u16 `value` into an `Int` and negates it.**

```rust
public fun neg_from_u16(value: u16): Int
```

* **@param value:** The u16 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">neg\_from\_u32</mark>

**It wraps a u32 `value` into an `Int` and negates it.**

```rust
public fun neg_from_u32(value: u32): Int
```

* **@param value:** The u32 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">neg\_from\_u64</mark>

**It wraps a u64 `value` into an `Int` and negates it.**

```rust
public fun neg_from_u64(value: u64): Int
```

* **@param value:** The u64 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">neg\_from\_u128</mark>

**It wraps a u128 `value` into an `Int` and negates it.**

```rust
public fun neg_from_u128(value: u128): Int
```

* **@param value:** The u128 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">neg\_from\_u256</mark>

**It wraps a u256 `value` into an `Int` and negates it.**

```rust
public fun neg_from_u256(value: u256): Int
```

* **@param value:** The u256 value to wrap
* **@return Int.** The wrapped `value`

### <mark style="color:blue;">to\_u8</mark>

**It unwraps the value inside `self` and casts it to u8.**

```rust
public fun to_u8(self: Int): u8
```

* **@param self:** The Int struct.
* **@return u8.** The inner value cast to u8.

**Abort**

* `self.value` is negative

### <mark style="color:blue;">to\_u16</mark>

**It unwraps the value inside `self` and casts it to u16.**

```rust
public fun to_u16(self: Int): u16
```

* **@param self:** The Int struct.
* **@return u16.** The inner value cast to u16.

**Abort**

* `self.value` is negative

### <mark style="color:blue;">to\_u32</mark>

**It unwraps the value inside `self` and casts it to u32.**

```rust
public fun to_u32(self: Int): u32
```

* **@param self:** The Int struct.
* **@return u32.** The inner value cast to u32.

**Abort**

* `self.value` is negative

### <mark style="color:blue;">to\_u64</mark>

**It unwraps the value inside `self` and casts it to u64.**

```rust
public fun to_u64(self: Int): u64
```

* **@param self:** The Int struct.
* **@return u64.** The inner value cast to u64.

**Abort**

* `self.value` is negative

### <mark style="color:blue;">to\_u128</mark>

**It unwraps the value inside `self` and casts it to u128.**

```rust
public fun to_u128(self: Int): u128
```

* **@param self:** The Int struct.
* **@return u128.** The inner value cast to u128.

**Abort**

* `self.value` is negative

### <mark style="color:blue;">to\_u256</mark>

**It unwraps the value inside `self` and casts it to u256.**

```rust
public fun to_u256(self: Int): u256
```

* **@param self:** The Int struct.
* **@return u128.** The inner value cast to u256.

**Abort**

* `self.value` is negative

### <mark style="color:blue;">truncate\_to\_u8</mark>

**It unwraps the value inside `self` and truncates it to u8.**

```rust
public fun truncate_to_u8(self: Int): u8
```

* **@param self:** The Int struct.
* **@return u8.** The inner value is truncated to u8.

### <mark style="color:blue;">truncate\_to\_u16</mark>

**It unwraps the value inside `self` and truncates it to u16.**

```rust
public fun truncate_to_u16(self: Int): u16
```

* **@param self:** The Int struct.
* **@return u8.** The inner value is truncated to u16.

### <mark style="color:blue;">truncate\_to\_u32</mark>

**It unwraps the value inside `self` and truncates it to u16.**

```rust
public fun truncate_to_u32(self: Int): u32
```

* **@param self:** The Int struct.
* **@return u8.** The inner value is truncated to u32.

### <mark style="color:blue;">truncate\_to\_u64</mark>

**It unwraps the value inside `self` and truncates it to u64.**

```rust
public fun truncate_to_u64(self: Int): u64
```

* **@param self:** The Int struct.
* **@return u8.** The inner value is truncated to u64.

### <mark style="color:blue;">truncate\_to\_u128</mark>

**It unwraps the value inside `self` and truncates it to u128.**

```rust
public fun truncate_to_u128(self: Int): u128
```

* **@param self:** The Int struct.
* **@return u8.** The inner value is truncated to u128.

### <mark style="color:blue;">flip</mark>

**It flips the sign of `self`.**

```rust
public fun flip(self: Int): Int
```

* **@param self:** The Int struct.
* **@return Int.** The returned Int will have its signed flipped.

### <mark style="color:blue;">abs</mark>

**It returns the absolute of an Int.**

```rust
public fun abs(self: Int): Int
```

* **@param self:** The Int struct.
* **@return Int.** The absolute.

### <mark style="color:blue;">is\_neg</mark>

**It checks if `self` is negative.**

```rust
public fun is_neg(self: Int): bool
```

* **@param self:** The Int struct.
* **@return bool.**

### <mark style="color:blue;">is\_zero</mark>

**It checks if `self` is zero.**

```rust
public fun is_zero(self: Int): bool
```

* **@param self:** The Int struct.
* **@return bool.**

### <mark style="color:blue;">is\_positive</mark>

**It checks if `self` is positive.**

```rust
public fun is_positive(self: Int): bool
```

* **@param self:** The Int struct.
* **@return bool.**

### <mark style="color:blue;">compare</mark>

**It compares `a` and `b`.**

```rust
public fun compare(a: Int, b: Int): u8
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return 0**. a == b.
* **@return 1.** a < b.
* **@return 2.** a > b.

### <mark style="color:blue;">eq</mark>

**It checks if `a` and `b` are equal.**

```rust
public fun eq(a: Int, b: Int): bool
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return bool.**

### <mark style="color:blue;">lt</mark>

**It checks if `a` < `b`.**

```rust
public fun lt(a: Int, b: Int): bool
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return bool.**

### <mark style="color:blue;">lte</mark>

**It checks if `a` <= `b`.**

```rust
public fun lte(a: Int, b: Int): bool
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return bool.**

### <mark style="color:blue;">gt</mark>

**It checks if `a` > `b`.**

```rust
public fun gt(a: Int, b: Int): bool
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return bool.**

### <mark style="color:blue;">gte</mark>

**It checks if `a` >= `b`.**

```rust
public fun gte(a: Int, b: Int): bool
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return bool.**

### <mark style="color:blue;">add</mark>

**It checks if `a` >= `b`.**

```rust
public fun add(a: Int, b: Int): Int
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return Int.** The result of `a` + `b`.

### <mark style="color:blue;">sub</mark>

**It performs `a` - `b.`**

```rust
public fun sub(a: Int, b: Int): Int
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return Int.** The result of `a` - `b`.

### <mark style="color:blue;">mul</mark>

**It performs `a` \* `b.`**

```rust
public fun mul(a: Int, b: Int): Int
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return Int.** The result of `a` \* `b`.

### <mark style="color:blue;">div\_down</mark>

**It performs `a` / `b` rounding down.**

```rust
public fun div_down(a: Int, b: Int): Int
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return Int.** The result of `a` / `b` rounding down.

### <mark style="color:blue;">div\_up</mark>

**It performs `a` / `b` rounding up.**

```rust
public fun div_up(a: Int, b: Int): Int
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return Int.** The result of `a` / `b` rounding up.

### <mark style="color:blue;">mod</mark>

**It performs `a` % `b`.**

```rust
public fun mod(a: Int, b: Int): Int
```

* **@param a:** An Int struct.
* **@param b:** An Int struct.
* **@return Int.** The result of `a` % `b`.

### <mark style="color:blue;">pow</mark>

**It performs `base` \*\* `exponent`.**

```rust
public fun pow(base: Int, exponent: u256): Int
```

* **@param base:** An Int struct.
* **@param exponent:** The exponent.
* **@return Int.** The result of `base` \*\* `exponent`.

### <mark style="color:blue;">shr</mark>

**It performs `self` >> `rhs`.**

```rust
public fun shr(self: Int, rhs: u8): Int
```

* **@param self:** An Int struct.
* **@param rhs:** The value to right-hand shift.
* **@return Int.** The result of `self` >> `rhs`.

### <mark style="color:blue;">shl</mark>

**It performs `self` << `lhs`.**

```rust
public fun shl(self: Int, lhs: u8): Int
```

* **@param self:** An Int struct.
* **@param lhs:** The value to right-hand shift.
* **@return Int.** The result of `self` << `lhs`.

### <mark style="color:blue;">or</mark>

**It performs `a` | `b`.**

```rust
public fun or(a: Int, b: Int): Int
```

* **@param a:** The first operand.
* **@param b:** The second operand.
* **@return Int.** The result of `a` | `b`.

### <mark style="color:blue;">and</mark>

**It performs `a` & `b`.**

```rust
public fun and(a: Int, b: Int): Int
```

* **@param a:** The first operand.
* **@param b:** The second operand.
* **@return Int.** The result of `a` & `b`.
