Provides utility functions on 64-bit unsigned integers.
:::note
Most operations on integer numbers (e.g. addition) are available as built-in operators (e.g. 1 + 1).
This module provides equivalent functions and Text conversion.
:::
:::info Function form for higher-order use
Several arithmetic and comparison functions (e.g. add, sub, equal, less, pow) are defined in this module to enable their use as first-class function values, which is not possible with operators like +, -, ==, etc., in Motoko. This allows you to pass these operations to higher-order functions such as map, foldLeft, or sort.
:::
Import from the base library to use this module.
motoko name=import
import Nat64 "mo:base/Nat64";64-bit natural numbers.
public let maximumValue : Nat64Maximum 64-bit natural number. 2 ** 64 - 1.
Example:
motoko include=import
Nat64.maximumValue; // => 18446744073709551615 : Nat64public func toNat(_ : Nat64) : NatConverts a 64-bit unsigned integer to an unsigned integer with infinite precision.
Example:
motoko include=import
Nat64.toNat(123); // => 123 : Natpublic func fromNat(_ : Nat) : Nat64Converts an unsigned integer with infinite precision to a 64-bit unsigned integer.
Traps on overflow.
Example:
motoko include=import
Nat64.fromNat(123); // => 123 : Nat64public func fromNat32(x : Nat32) : Nat64Converts a 32-bit unsigned integer to a 64-bit unsigned integer.
Example:
motoko include=import
Nat64.fromNat32(123); // => 123 : Nat64public func toNat32(x : Nat64) : Nat32Converts a 64-bit unsigned integer to a 32-bit unsigned integer.
Traps on overflow.
Example:
motoko include=import
Nat64.toNat32(123); // => 123 : Nat32public func fromIntWrap(_ : Int) : Nat64Converts a signed integer with infinite precision to a 64-bit unsigned integer.
Traps on overflow/underflow.
Example:
motoko include=import
Nat64.fromIntWrap(123); // => 123 : Nat64public func toText(x : Nat64) : TextConverts x to its textual representation. Textual representation do not
contain underscores to represent commas.
Example:
motoko include=import
Nat64.toText(1234); // => "1234" : Textpublic func min(x : Nat64, y : Nat64) : Nat64Returns the minimum of x and y.
Example:
motoko include=import
Nat64.min(123, 456); // => 123 : Nat64public func max(x : Nat64, y : Nat64) : Nat64Returns the maximum of x and y.
Example:
motoko include=import
Nat64.max(123, 456); // => 456 : Nat64public func equal(x : Nat64, y : Nat64) : BoolEquality function for Nat64 types.
This is equivalent to x == y.
Example:
motoko include=import
ignore Nat64.equal(1, 1); // => true
(1 : Nat64) == (1 : Nat64) // => true
Example:
motoko include=import
import Buffer "mo:base/Buffer";
let buffer1 = Buffer.Buffer<Nat64>(3);
let buffer2 = Buffer.Buffer<Nat64>(3);
Buffer.equal(buffer1, buffer2, Nat64.equal) // => truepublic func notEqual(x : Nat64, y : Nat64) : BoolInequality function for Nat64 types.
This is equivalent to x != y.
Example:
motoko include=import
ignore Nat64.notEqual(1, 2); // => true
(1 : Nat64) != (2 : Nat64) // => truepublic func less(x : Nat64, y : Nat64) : Bool"Less than" function for Nat64 types.
This is equivalent to x < y.
Example:
motoko include=import
ignore Nat64.less(1, 2); // => true
(1 : Nat64) < (2 : Nat64) // => truepublic func lessOrEqual(x : Nat64, y : Nat64) : Bool"Less than or equal" function for Nat64 types.
This is equivalent to x <= y.
Example:
motoko include=import
ignore Nat64.lessOrEqual(1, 2); // => true
(1 : Nat64) <= (2 : Nat64) // => truepublic func greater(x : Nat64, y : Nat64) : Bool"Greater than" function for Nat64 types.
This is equivalent to x > y.
Example:
motoko include=import
ignore Nat64.greater(2, 1); // => true
(2 : Nat64) > (1 : Nat64) // => truepublic func greaterOrEqual(x : Nat64, y : Nat64) : Bool"Greater than or equal" function for Nat64 types.
This is equivalent to x >= y.
Example:
motoko include=import
ignore Nat64.greaterOrEqual(2, 1); // => true
(2 : Nat64) >= (1 : Nat64) // => truepublic func compare(x : Nat64, y : Nat64) : {#less; #equal; #greater}General purpose comparison function for Nat64. Returns the Order (
either #less, #equal, or #greater) of comparing x with y.
Example:
motoko include=import
Nat64.compare(2, 3) // => #less
This function can be used as value for a high order function, such as a sort function.
Example:
motoko include=import
import Array "mo:base/Array";
Array.sort([2, 3, 1] : [Nat64], Nat64.compare) // => [1, 2, 3]public func add(x : Nat64, y : Nat64) : Nat64Returns the sum of x and y, x + y.
Traps on overflow.
Example:
motoko include=import
ignore Nat64.add(1, 2); // => 3
(1 : Nat64) + (2 : Nat64) // => 3
Example:
motoko include=import
import Array "mo:base/Array";
Array.foldLeft<Nat64, Nat64>([2, 3, 1], 0, Nat64.add) // => 6public func sub(x : Nat64, y : Nat64) : Nat64Returns the difference of x and y, x - y.
Traps on underflow.
Example:
motoko include=import
ignore Nat64.sub(3, 1); // => 2
(3 : Nat64) - (1 : Nat64) // => 2
Example:
motoko include=import
import Array "mo:base/Array";
Array.foldLeft<Nat64, Nat64>([2, 3, 1], 10, Nat64.sub) // => 4public func mul(x : Nat64, y : Nat64) : Nat64Returns the product of x and y, x * y.
Traps on overflow.
Example:
motoko include=import
ignore Nat64.mul(2, 3); // => 6
(2 : Nat64) * (3 : Nat64) // => 6
Example:
motoko include=import
import Array "mo:base/Array";
Array.foldLeft<Nat64, Nat64>([2, 3, 1], 1, Nat64.mul) // => 6public func div(x : Nat64, y : Nat64) : Nat64Returns the quotient of x divided by y, x / y.
Traps when y is zero.
Example:
motoko include=import
ignore Nat64.div(6, 2); // => 3
(6 : Nat64) / (2 : Nat64) // => 3public func rem(x : Nat64, y : Nat64) : Nat64Returns the remainder of x divided by y, x % y.
Traps when y is zero.
Example:
motoko include=import
ignore Nat64.rem(6, 4); // => 2
(6 : Nat64) % (4 : Nat64) // => 2public func pow(x : Nat64, y : Nat64) : Nat64Returns x to the power of y, x ** y. Traps on overflow.
Example:
motoko include=import
ignore Nat64.pow(2, 3); // => 8
(2 : Nat64) ** (3 : Nat64) // => 8public func bitnot(x : Nat64) : Nat64Returns the bitwise negation of x, ^x.
Example:
motoko include=import
ignore Nat64.bitnot(0); // => 18446744073709551615
^(0 : Nat64) // => 18446744073709551615public func bitand(x : Nat64, y : Nat64) : Nat64Returns the bitwise and of x and y, x & y.
Example:
motoko include=import
ignore Nat64.bitand(1, 3); // => 1
(1 : Nat64) & (3 : Nat64) // => 1public func bitor(x : Nat64, y : Nat64) : Nat64Returns the bitwise or of x and y, x | y.
Example:
motoko include=import
ignore Nat64.bitor(1, 3); // => 3
(1 : Nat64) | (3 : Nat64) // => 3public func bitxor(x : Nat64, y : Nat64) : Nat64Returns the bitwise exclusive or of x and y, x ^ y.
Example:
motoko include=import
ignore Nat64.bitxor(1, 3); // => 2
(1 : Nat64) ^ (3 : Nat64) // => 2public func bitshiftLeft(x : Nat64, y : Nat64) : Nat64Returns the bitwise shift left of x by y, x << y.
Example:
motoko include=import
ignore Nat64.bitshiftLeft(1, 3); // => 8
(1 : Nat64) << (3 : Nat64) // => 8public func bitshiftRight(x : Nat64, y : Nat64) : Nat64Returns the bitwise shift right of x by y, x >> y.
Example:
motoko include=import
ignore Nat64.bitshiftRight(8, 3); // => 1
(8 : Nat64) >> (3 : Nat64) // => 1public func bitrotLeft(x : Nat64, y : Nat64) : Nat64Returns the bitwise rotate left of x by y, x <<> y.
Example:
motoko include=import
ignore Nat64.bitrotLeft(1, 3); // => 8
(1 : Nat64) <<> (3 : Nat64) // => 8public func bitrotRight(x : Nat64, y : Nat64) : Nat64Returns the bitwise rotate right of x by y, x <>> y.
Example:
motoko include=import
ignore Nat64.bitrotRight(8, 3); // => 1
(8 : Nat64) <>> (3 : Nat64) // => 1public func bittest(x : Nat64, p : Nat) : BoolReturns the value of bit p mod 64 in x, (x & 2^(p mod 64)) == 2^(p mod 64).
This is equivalent to checking if the p-th bit is set in x, using 0 indexing.
Example:
motoko include=import
Nat64.bittest(5, 2); // => truepublic func bitset(x : Nat64, p : Nat) : Nat64Returns the value of setting bit p mod 64 in x to 1.
Example:
motoko include=import
Nat64.bitset(5, 1); // => 7public func bitclear(x : Nat64, p : Nat) : Nat64Returns the value of clearing bit p mod 64 in x to 0.
Example:
motoko include=import
Nat64.bitclear(5, 2); // => 1public func bitflip(x : Nat64, p : Nat) : Nat64Returns the value of flipping bit p mod 64 in x.
Example:
motoko include=import
Nat64.bitflip(5, 2); // => 1public func bitcountNonZero(x : Nat64) : Nat64Returns the count of non-zero bits in x.
Example:
motoko include=import
Nat64.bitcountNonZero(5); // => 2public func bitcountLeadingZero(x : Nat64) : Nat64Returns the count of leading zero bits in x.
Example:
motoko include=import
Nat64.bitcountLeadingZero(5); // => 61public func bitcountTrailingZero(x : Nat64) : Nat64Returns the count of trailing zero bits in x.
Example:
motoko include=import
Nat64.bitcountTrailingZero(16); // => 4public func explode(x : Nat64) : (msb : Nat8, Nat8, Nat8, Nat8, Nat8, Nat8, Nat8, lsb : Nat8)Returns the upper (i.e. most significant), lower (least significant)
and in-between bytes of x.
Example:
motoko include=import
Nat64.explode 0xbb772266aa885511 // => (187, 119, 34, 102, 170, 136, 85, 17)public func addWrap(x : Nat64, y : Nat64) : Nat64Returns the sum of x and y, x +% y. Wraps on overflow.
Example:
motoko include=import
ignore Nat64.addWrap(Nat64.maximumValue, 1); // => 0
Nat64.maximumValue +% (1 : Nat64) // => 0
:::info
The reason why this function is defined in this library (in addition
to the existing +% operator) is so that you can use it as a function
value to pass to a higher order function. It is not possible to use +%
as a function value at the moment.
:::
public func subWrap(x : Nat64, y : Nat64) : Nat64Returns the difference of x and y, x -% y. Wraps on underflow.
Example:
motoko include=import
ignore Nat64.subWrap(0, 1); // => 18446744073709551615
(0 : Nat64) -% (1 : Nat64) // => 18446744073709551615