pub struct I16 {
    /// The underlying value representing the signed integer.
    underlying: u16,
}
Expand description

The 16-bit signed integer type.

Additional Information

Represented as an underlying u16 value.
Actual value is underlying value minus 2 ^ 15
Max value is 2 ^ 15 - 1, min value is - 2 ^ 15

Fields

underlying: u16

The underlying value representing the signed integer.

Implementations

fn indent() -> u16

The underlying value that corresponds to zero value.

Returns

[u16] - The unsigned integer value representing a zero value.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let zero = I16::indent();
    assert(zero == 32768u16);
}

fn bits() -> u64

The size of this type in bits.

Returns

[u64] - The defined size of the I16 type.

Examples

``sway
use sway_libs::signed_integers::i16::I16;

fn foo() {
let bits = I16::bits();
assert(bits == 16);
}

fn from_uint(underlying: u16) -> Self

Helper function to get a positive value from an unsigned number

Arguments

  • underlying: [u16] - The unsigned number to become the underlying value for the I16.

Returns

  • [I16] - The newly created I16 struct.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let underlying = 1u16;
    let i16 = I16::from_uint(underlying);
    assert(i16.underlying() == underlying);
}

fn max() -> Self

The largest value that can be represented by this integer type.

Returns

  • [I16] - The newly created I16 struct.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let i16 = I16::max();
    assert(i16.underlying() == u16::max());
}

fn min() -> Self

The smallest value that can be represented by this integer type.

Returns

  • [I16] - The newly created I16 type.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let i16 = I16::min();
    assert(i16.underlying() == u16::min());
}

fn neg_from(value: u16) -> Self

Helper function to get a negative value of an unsigned number.

Arguments

  • value: [u16] - The unsigned number to negate.

Returns

  • [I16] - The newly created I16 struct.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let underlying = 1u16;
    let i16 = I16::neg_from(underlying);
    assert(i16.underlying() == 32767u16)
}

fn new() -> Self

Initializes a new, zeroed I16.

Additional Information

The zero value of I16 is 32768u16.

Returns

  • [I16] - The newly created I16 struct.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let i16 = I16::new();
    assert(i16.underlying() == 32768u16);
}

fn zero() -> Self

The zero value I16.

Returns

  • [I16] - The newly created I16 type.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let i16 = I16::zero();
    assert(i16.underlying() == 32768u16);
}

fn is_zero(self) -> bool

Returns whether a I16 is set to zero.

Returns

  • [bool] -> True if the I16 is zero, otherwise false.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let i16 = I16::zero();
    assert(i16.is_zero());
}

fn underlying(self) -> u16

Returns the underlying u16 representing the I16.

Returns

  • [u16] - The u16 representing the I16.

Examples

use sway_libs::signed_integers::i16::I16;

fn foo() {
    let i16 = I16::zero();
    assert(i16.underlying() == 32768u16);
}

Trait Implementations

fn from(value: u16) -> Self

Helper function to get a signed number from with an underlying

fn eq(self, other: Self) -> bool

fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}

fn gt(self, other: Self) -> bool

fn lt(self, other: Self) -> bool

fn add(self, other: Self) -> Self

Add a I16 to a I16. Panics on overflow.

fn divide(self, divisor: Self) -> Self

Divide a I16 by a I16. Panics if divisor is zero.

fn multiply(self, other: Self) -> Self

Multiply a I16 with a I16. Panics of overflow.

fn subtract(self, other: Self) -> Self

Subtract a I16 from a I16. Panics of overflow.

fn twos_complement(self) -> Self