Skip to main content

Int8

8-bit signed integers with checked arithmetic.

Common 8-bit integer functions. Most operations are available as built-in operators (e.g. 1 + 1).

Type Int8

type Int8 = Prim.Types.Int8

8-bit signed integers.

Value minimumValue

let minimumValue : Int8

Minimum 8-bit integer value, -2 ** 7.

Value maximumValue

let maximumValue : Int8

Maximum 8-bit integer value, +2 ** 7 - 1.

Value toInt

let toInt : Int8 -> Int

Converts a 8-bit signed integer to a signed integer with infinite precision.

Example:

import Int8 "mo:base/Int8";

Int8.toInt(123) // => 123 : Int

Value fromInt

let fromInt : Int -> Int8

Converts a signed integer with infinite precision to a 8-bit signed integer.

Traps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.fromInt(123) // => +123 : Int8

Value fromIntWrap

let fromIntWrap : Int -> Int8

Converts a signed integer with infinite precision to a 8-bit signed integer.

Wraps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.fromIntWrap(-123) // => -123 : Int

Value fromNat8

let fromNat8 : Nat8 -> Int8

Converts an unsigned 8-bit integer to a signed 8-bit integer.

Wraps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.fromNat8(123) // => +123 : Int8

Value toNat8

let toNat8 : Int8 -> Nat8

Converts a signed 8-bit integer to an unsigned 8-bit integer.

Wraps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.toNat8(-1) // => 255 : Nat8 // underflow

Function toText

func toText(x : Int8) : Text

Returns the Text representation of x. Formats the integer in decimal representation.

Example:

import Int8 "mo:base/Int8";

Int8.toText(-123) // => "-123"

Function abs

func abs(x : Int8) : Int8

Returns the absolute value of x.

Traps when x == -2 ** 7 (the minimum Int8 value).

Example:

import Int8 "mo:base/Int8";

Int8.abs(-123) // => +123

Function min

func min(x : Int8, y : Int8) : Int8

Returns the minimum of x and y.

Example:

import Int8 "mo:base/Int8";

Int8.min(+2, -3) // => -3

Function max

func max(x : Int8, y : Int8) : Int8

Returns the maximum of x and y.

Example:

import Int8 "mo:base/Int8";

Int8.max(+2, -3) // => +2

Function equal

func equal(x : Int8, y : Int8) : Bool

Returns x == y.

Example:

import Int8 "mo:base/Int8";

Int8.equal(123, 123) // => true

Function notEqual

func notEqual(x : Int8, y : Int8) : Bool

Returns x != y.

Example:

import Int8 "mo:base/Int8";

Int8.notEqual(123, 123) // => false

Function less

func less(x : Int8, y : Int8) : Bool

Returns x < y.

Example:

import Int8 "mo:base/Int8";

Int8.less(123, 124) // => true

Function lessOrEqual

func lessOrEqual(x : Int8, y : Int8) : Bool

Returns x <= y.

Example:

import Int8 "mo:base/Int8";

Int8.lessOrEqual(123, 124) // => true

Function greater

func greater(x : Int8, y : Int8) : Bool

Returns x > y.

Example:

import Int8 "mo:base/Int8";

Int8.greater(124, 123) // => true

Function greaterOrEqual

func greaterOrEqual(x : Int8, y : Int8) : Bool

Returns x >= y.

Example:

import Int8 "mo:base/Int8";

Int8.greaterOrEqual(124, 123) // => true

Function compare

func compare(x : Int8, y : Int8) : {#less; #equal; #greater}

Returns the order of x and y.

Example:

import Int8 "mo:base/Int8";

Int8.compare(123, 124) // => #less

Function neg

func neg(x : Int8) : Int8

Returns the negation of x, -x.

Traps on overflow, i.e. for neg(-2 ** 7).

Example:

import Int8 "mo:base/Int8";

Int8.neg(123) // => -123

Function add

func add(x : Int8, y : Int8) : Int8

Returns the sum of x and y, x + y.

Traps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.add(100, 23) // => +123

Function sub

func sub(x : Int8, y : Int8) : Int8

Returns the difference of x and y, x - y.

Traps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.sub(123, 23) // => +100

Function mul

func mul(x : Int8, y : Int8) : Int8

Returns the product of x and y, x * y.

Traps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.mul(12, 10) // => +120

Function div

func div(x : Int8, y : Int8) : Int8

Returns the signed integer division of x by y, x / y. Rounds the quotient towards zero, which is the same as truncating the decimal places of the quotient.

Traps when y is zero.

Example:

import Int8 "mo:base/Int8";

Int8.div(123, 10) // => +12

Function rem

func rem(x : Int8, y : Int8) : Int8

Returns the remainder of the signed integer division of x by y, x % y, which is defined as x - x / y * y.

Traps when y is zero.

Example:

import Int8 "mo:base/Int8";

Int8.rem(123, 10) // => +3

Function pow

func pow(x : Int8, y : Int8) : Int8

Returns x to the power of y, x ** y.

Traps on overflow/underflow and when y < 0 or y >= 8.

Example:

import Int8 "mo:base/Int8";

Int8.pow(2, 6) // => +64

Function bitnot

func bitnot(x : Int8) : Int8

Returns the bitwise negation of x, ^x.

Example:

import Int8 "mo:base/Int8";

Int8.bitnot(-16 /* 0xf0 */) // => +15 // 0x0f

Function bitand

func bitand(x : Int8, y : Int8) : Int8

Returns the bitwise "and" of x and y, x & y.

Example:

import Int8 "mo:base/Int8";

Int8.bitand(0x1f, 0x70) // => +16 // 0x10

Function bitor

func bitor(x : Int8, y : Int8) : Int8

Returns the bitwise "or" of x and y, x | y.

Example:

import Int8 "mo:base/Int8";

Int8.bitor(0x0f, 0x70) // => +127 // 0x7f

Function bitxor

func bitxor(x : Int8, y : Int8) : Int8

Returns the bitwise "exclusive or" of x and y, x ^ y.

Example:

import Int8 "mo:base/Int8";

Int8.bitxor(0x70, 0x7f) // => +15 // 0x0f

Function bitshiftLeft

func bitshiftLeft(x : Int8, y : Int8) : Int8

Returns the bitwise left shift of x by y, x << y. The right bits of the shift filled with zeros. Left-overflowing bits, including the sign bit, are discarded.

For y >= 8, the semantics is the same as for bitshiftLeft(x, y % 8). For y < 0, the semantics is the same as for bitshiftLeft(x, y + y % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bitshiftLeft(1, 4) // => +16 // 0x10 equivalent to `2 ** 4`.

Function bitshiftRight

func bitshiftRight(x : Int8, y : Int8) : Int8

Returns the signed bitwise right shift of x by y, x >> y. The sign bit is retained and the left side is filled with the sign bit. Right-underflowing bits are discarded, i.e. not rotated to the left side.

For y >= 8, the semantics is the same as for bitshiftRight(x, y % 8). For y < 0, the semantics is the same as for bitshiftRight (x, y + y % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bitshiftRight(64, 4) // => +4 // equivalent to `64 / (2 ** 4)`

Function bitrotLeft

func bitrotLeft(x : Int8, y : Int8) : Int8

Returns the bitwise left rotatation of x by y, x <<> y. Each left-overflowing bit is inserted again on the right side. The sign bit is rotated like other bits, i.e. the rotation interprets the number as unsigned.

Changes the direction of rotation for negative y. For y >= 8, the semantics is the same as for bitrotLeft(x, y % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bitrotLeft(0x11 /* 0b0001_0001 */, 2) // => +68 // 0b0100_0100 == 0x44.

Function bitrotRight

func bitrotRight(x : Int8, y : Int8) : Int8

Returns the bitwise right rotation of x by y, x <>> y. Each right-underflowing bit is inserted again on the right side. The sign bit is rotated like other bits, i.e. the rotation interprets the number as unsigned.

Changes the direction of rotation for negative y. For y >= 8, the semantics is the same as for bitrotRight(x, y % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bitrotRight(0x11 /* 0b0001_0001 */, 1) // => -120 // 0b1000_1000 == 0x88.

Function bittest

func bittest(x : Int8, p : Nat) : Bool

Returns the value of bit p in x, x & 2**p == 2**p. If p >= 8, the semantics is the same as for bittest(x, p % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bittest(64, 6) // => true

Function bitset

func bitset(x : Int8, p : Nat) : Int8

Returns the value of setting bit p in x to 1. If p >= 8, the semantics is the same as for bitset(x, p % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bitset(0, 6) // => +64

Function bitclear

func bitclear(x : Int8, p : Nat) : Int8

Returns the value of clearing bit p in x to 0. If p >= 8, the semantics is the same as for bitclear(x, p % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bitclear(-1, 6) // => -65

Function bitflip

func bitflip(x : Int8, p : Nat) : Int8

Returns the value of flipping bit p in x. If p >= 8, the semantics is the same as for bitclear(x, p % 8).

Example:

import Int8 "mo:base/Int8";

Int8.bitflip(127, 6) // => +63

Value bitcountNonZero

let bitcountNonZero : (x : Int8) -> Int8

Returns the count of non-zero bits in x.

Example:

import Int8 "mo:base/Int8";

Int8.bitcountNonZero(0x0f) // => +4

Value bitcountLeadingZero

let bitcountLeadingZero : (x : Int8) -> Int8

Returns the count of leading zero bits in x.

Example:

import Int8 "mo:base/Int8";

Int8.bitcountLeadingZero(0x08) // => +4

Value bitcountTrailingZero

let bitcountTrailingZero : (x : Int8) -> Int8

Returns the count of trailing zero bits in x.

Example:

import Int8 "mo:base/Int8";

Int8.bitcountTrailingZero(0x10) // => +4

Function addWrap

func addWrap(x : Int8, y : Int8) : Int8

Returns the sum of x and y, x +% y.

Wraps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.addWrap(2 ** 6, 2 ** 6) // => -128 // overflow

Function subWrap

func subWrap(x : Int8, y : Int8) : Int8

Returns the difference of x and y, x -% y.

Wraps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.subWrap(-2 ** 7, 1) // => +127 // underflow

Function mulWrap

func mulWrap(x : Int8, y : Int8) : Int8

Returns the product of x and y, x *% y. Wraps on overflow.

Wraps on overflow/underflow.

Example:

import Int8 "mo:base/Int8";

Int8.mulWrap(2 ** 4, 2 ** 4) // => 0 // overflow

Function powWrap

func powWrap(x : Int8, y : Int8) : Int8

Returns x to the power of y, x **% y.

Wraps on overflow/underflow. Traps if y < 0 or y >= 8.

Example:

import Int8 "mo:base/Int8";

Int8.powWrap(2, 7) // => -128 // overflow