Standard library · Numbers and collections

bigint

Whole numbers of unlimited size: large factorials, cryptography, and any arithmetic that will not fit in 64 bits.

import bigint
On this page
  1. Types
  2. Functions

An i64 holds about nine quintillion. For anything larger, such as the factorial of 30 or a cryptographic key, use bigint.Int, whose size is limited only by memory.

Most functions return a new number and leave their arguments alone. The ones whose names say To, From or By, such as AddTo, change the number in place and are faster in a loop because they take no new memory. These numbers allocate; release the ones you no longer need with Free.

Types

Int

struct Int
A whole number of any size. Build one with a function such as FromInt or Parse.
NameTypeDescription
negbool = falseTrue when the number is negative.
magVectorThe absolute value, as 32-bit limbs from least to most significant.
bigint-Int.salam
import bigint

func main:
    mut a := bigint.FromInt(123456789)
    mut b := bigint.Pow(bigint.FromInt(2), 100)
    defer a.free()
    defer b.free()
    println b.to_str()
    println a.is_zero(), a.sign(), a.is_odd(), a.is_even()
    println b.bit_len(), b.limb_count()
    c := a.clone()
    println c.to_str()
end
Output1267650600228229401496703205376 false 1 true false 101 4 123456789

free

func free()
Releases the number's memory.

The example for this item is shown with the ones above: bigint-Int.salam

is_zero

func is_zero(): bool
True when the number is zero.

The example for this item is shown with the ones above: bigint-Int.salam

sign

func sign(): int
The sign: minus one, zero or one.

The example for this item is shown with the ones above: bigint-Int.salam

is_odd

func is_odd(): bool
True when the number is odd.

The example for this item is shown with the ones above: bigint-Int.salam

is_even

func is_even(): bool
True when the number is even.

The example for this item is shown with the ones above: bigint-Int.salam

bit_len

func bit_len(): int
How many bits the absolute value needs.

The example for this item is shown with the ones above: bigint-Int.salam

limb_count

func limb_count(): int
How many 32-bit limbs the number occupies.

The example for this item is shown with the ones above: bigint-Int.salam

clone

func clone(): Int
An independent copy of the number.

The example for this item is shown with the ones above: bigint-Int.salam

to_str

func to_str(): str
The number as decimal text.

The example for this item is shown with the ones above: bigint-Int.salam

Functions

And

func And(a: Int, b: Int): Int
The bitwise and of two numbers.
bigint-And.salam
import bigint

func main:
    a := bigint.FromInt(12)
    b := bigint.FromInt(10)
    println bigint.ToStr(bigint.And(a, b)), bigint.ToStr(bigint.Or(a, b)), bigint.ToStr(bigint.Xor(a, b))
    println bigint.ToStr(bigint.AndNot(a, b)), bigint.ToStr(bigint.Not(a))
    println bigint.PopCount(a), bigint.TrailingZeros(a)
end
Output8 14 6 4 -13 2 2

Or

func Or(a: Int, b: Int): Int
The bitwise or of two numbers.

The example for this item is shown with the ones above: bigint-And.salam

Xor

func Xor(a: Int, b: Int): Int
The bitwise exclusive or of two numbers.

The example for this item is shown with the ones above: bigint-And.salam

AndNot

func AndNot(a: Int, b: Int): Int
The bits of the first number that are zero in the second.

The example for this item is shown with the ones above: bigint-And.salam

Not

func Not(x: Int): Int
The bitwise complement; it behaves as two's complement for negative numbers, so the complement of 12 is −13.

The example for this item is shown with the ones above: bigint-And.salam

PopCount

func PopCount(x: Int): int
How many one bits the absolute value has.

The example for this item is shown with the ones above: bigint-And.salam

TrailingZeros

func TrailingZeros(x: Int): int
How many zero bits there are in a row from the least significant end.

The example for this item is shown with the ones above: bigint-And.salam

Zero

func Zero(): Int
The number zero.
bigint-Add.salam
import bigint

func main:
    a := bigint.FromInt(9000000000000000000)
    b := bigint.FromUint(18000000000000000000 as u64)
    println bigint.ToStr(bigint.Add(a, b))
    println bigint.ToStr(bigint.Sub(a, b))
    println bigint.ToStr(bigint.Mul(a, b))
    println bigint.ToStr(bigint.Sqr(a))
    println bigint.ToStr(bigint.AddInt(a, 1)), bigint.ToStr(bigint.SubInt(a, 1)), bigint.ToStr(bigint.MulInt(a, 2))
    println bigint.ToStr(bigint.Neg(a)), bigint.ToStr(bigint.Abs(bigint.Neg(a)))
    println bigint.ToStr(bigint.Shl(bigint.One(), 70)), bigint.ToStr(bigint.Shr(a, 60))
    println bigint.ToStr(bigint.Zero()), bigint.ToStr(bigint.Pow10(20))
end
Output27000000000000000000 -9000000000000000000 162000000000000000000000000000000000000 81000000000000000000000000000000000000 9000000000000000001 8999999999999999999 18000000000000000000 -9000000000000000000 9000000000000000000 1180591620717411303424 7 0 100000000000000000000

One

func One(): Int
The number one.

The example for this item is shown with the ones above: bigint-Add.salam

FromInt

func FromInt(v: i64): Int
Builds a big number from an i64.

The example for this item is shown with the ones above: bigint-Int.salam

FromUint

func FromUint(v: u64): Int
Builds a big number from a u64.

The example for this item is shown with the ones above: bigint-Add.salam

Clone

func Clone(x: Int): Int
An independent copy of the number.
bigint-Cmp.salam
import bigint

func main:
    a := bigint.FromInt(5)
    b := bigint.FromInt(-9)
    println bigint.Cmp(a, b), bigint.CmpAbs(a, b)
    println bigint.Equals(a, a), bigint.Less(a, b), bigint.Greater(a, b)
    println bigint.ToStr(bigint.Min(a, b)), bigint.ToStr(bigint.Max(a, b))
    println bigint.EqualsInt(a, 5), bigint.IsZero(a), bigint.Sign(b)
    println bigint.IsOdd(a), bigint.IsEven(a), bigint.BitLen(a), bigint.Bit(a, 1)
    c := bigint.Clone(b)
    println bigint.ToStr(c)
end
Output1 -1 true false true -9 5 true false -1 true false 3 0 -9

Free

func Free(x &: Int)
Releases the number's memory.
bigint-AddTo.salam
import bigint

func main:
    mut acc := bigint.Zero()
    defer bigint.Free(acc)
    bigint.SetInt(acc, 10)
    bigint.AddTo(acc, bigint.FromInt(5))
    bigint.SubFrom(acc, bigint.FromInt(3))
    bigint.MulBy(acc, bigint.FromInt(4))
    println bigint.ToStr(acc)
    bigint.AddIntTo(acc, 2)
    bigint.MulIntBy(acc, 10)
    println bigint.ToStr(acc)
    bigint.NegateIn(acc)
    bigint.ShlBy(acc, 1)
    println bigint.ToStr(acc)
    bigint.ShrBy(acc, 2)
    println bigint.ToStr(acc)
    bigint.Set(acc, bigint.FromInt(7))
    println bigint.ToStr(acc)
end
Output48 500 -1000 -250 7

IsZero

func IsZero(x: Int): bool
True when the number is zero.

The example for this item is shown with the ones above: bigint-Cmp.salam

Sign

func Sign(x: Int): int
The sign: minus one, zero or one.

The example for this item is shown with the ones above: bigint-Cmp.salam

BitLen

func BitLen(x: Int): int
How many bits the absolute value needs.

The example for this item is shown with the ones above: bigint-Cmp.salam

Bit

func Bit(x: Int, i: int): int
Bit number i of the absolute value, zero or one.

The example for this item is shown with the ones above: bigint-Cmp.salam

IsOdd

func IsOdd(x: Int): bool
True when the number is odd.

The example for this item is shown with the ones above: bigint-Cmp.salam

IsEven

func IsEven(x: Int): bool
True when the number is even.

The example for this item is shown with the ones above: bigint-Cmp.salam

Cmp

func Cmp(a: Int, b: Int): int
Compares two numbers: minus one, zero or one.

The example for this item is shown with the ones above: bigint-Cmp.salam

CmpAbs

func CmpAbs(a: Int, b: Int): int
Compares the absolute values of two numbers.

The example for this item is shown with the ones above: bigint-Cmp.salam

Equals

func Equals(a: Int, b: Int): bool
True when the two numbers are equal.

The example for this item is shown with the ones above: bigint-Cmp.salam

Less

func Less(a: Int, b: Int): bool
True when the first is smaller.

The example for this item is shown with the ones above: bigint-Cmp.salam

Greater

func Greater(a: Int, b: Int): bool
True when the first is larger.

The example for this item is shown with the ones above: bigint-Cmp.salam

Min

func Min(a: Int, b: Int): Int
The smaller of two numbers.

The example for this item is shown with the ones above: bigint-Cmp.salam

Max

func Max(a: Int, b: Int): Int
The larger of two numbers.

The example for this item is shown with the ones above: bigint-Cmp.salam

EqualsInt

func EqualsInt(a: Int, v: i64): bool
True when the big number equals an i64.

The example for this item is shown with the ones above: bigint-Cmp.salam

ToStr

func ToStr(x: Int): str
The number as decimal text.

The example for this item is shown with the ones above: bigint-Add.salam

ToStrBase

func ToStrBase(x: Int, base: int): str
The number as text in a base of your choice, from 2 to 36.
bigint-Parse.salam
import bigint

func main:
    mut ok := false
    n := bigint.Parse("123456789012345678901234567890", ok)
    println ok, bigint.ToStr(n)
    h := bigint.ParseBase("ff", 16, ok)
    println ok, bigint.ToStr(h), bigint.ToStrBase(h, 2), bigint.ToHex(n)
    println bigint.ToStr(bigint.ParseOr("xyz", 10))
    mut fits := false
    v := bigint.ToInt(h, fits)
    println v, fits
    println bigint.ToIntClamped(n), bigint.ToFloat(n)
end
Outputtrue 123456789012345678901234567890 true 255 11111111 18ee90ff6c373e0ee4e3f0ad2 0 255 true 9223372036854775807 1.2345678901234568e+29

ToHex

func ToHex(x: Int): str
The number as base sixteen text.

The example for this item is shown with the ones above: bigint-Parse.salam

Parse

func Parse(s: str, ok &: bool): Int
Turns decimal text into a big number. When the text is not a valid number, ok turns false.

The example for this item is shown with the ones above: bigint-Parse.salam

ParseBase

func ParseBase(s: str, base: int, ok &: bool): Int
Turns text in a given base into a number.

The example for this item is shown with the ones above: bigint-Parse.salam

ParseOr

func ParseOr(s: str, base: int): Int
Parses text in the given base, returning zero when it cannot.

The example for this item is shown with the ones above: bigint-Parse.salam

ToInt

func ToInt(x: Int, ok &: bool): i64
Converts the number to an i64; when it does not fit, ok turns false.

The example for this item is shown with the ones above: bigint-Parse.salam

ToIntClamped

func ToIntClamped(x: Int): i64
Converts the number to an i64, giving the largest or smallest possible value when it does not fit.

The example for this item is shown with the ones above: bigint-Parse.salam

ToFloat

func ToFloat(x: Int): f64
Converts the number to the nearest f64; the least significant digits may be lost.

The example for this item is shown with the ones above: bigint-Parse.salam

ByteLen

func ByteLen(x: Int): int
How many bytes the absolute value needs.
bigint-Bytes.salam
import bigint
import mem

func main:
    n := bigint.FromInt(65536)
    size := bigint.ByteLen(n)
    println size
    buf := mem.Allocate(size as u64)
    println bigint.ToBytes(n, buf, size)
    back := bigint.FromBytes(buf, size)
    println bigint.ToStr(back)
    mem.Free(buf)
end
Output3 true 65536

FromBytes

func FromBytes(p: void*, n: int): Int
Builds the number from n bytes in memory, most significant first.

The example for this item is shown with the ones above: bigint-Bytes.salam

ToBytes

func ToBytes(x: Int, out: void*, n: int): bool
Writes the absolute value into n bytes of memory, most significant first; it returns false when it does not fit.

The example for this item is shown with the ones above: bigint-Bytes.salam

Pow

func Pow(x: Int, e: int): Int
The number raised to the power e.

The example for this item is shown with the ones above: bigint-Int.salam

Pow10

func Pow10(n: int): Int
Ten raised to the power n.

The example for this item is shown with the ones above: bigint-Add.salam

ModExp

func ModExp(base: Int, e: Int, m: Int): Int
Computes base to the power e modulo m quickly, without building the huge intermediate number; the basis of RSA.
bigint-ModExp.salam
import bigint

func main:
    println bigint.ToStr(bigint.Factorial(30))
    println bigint.ToStr(bigint.ModExp(bigint.FromInt(4), bigint.FromInt(13), bigint.FromInt(497)))
    println bigint.ToStr(bigint.Gcd(bigint.FromInt(48), bigint.FromInt(18))), bigint.ToStr(bigint.Lcm(bigint.FromInt(4), bigint.FromInt(6)))
    mut x := bigint.Zero()
    mut y := bigint.Zero()
    g := bigint.ExtGcd(bigint.FromInt(240), bigint.FromInt(46), x, y)
    println bigint.ToStr(g), bigint.ToStr(x), bigint.ToStr(y)
    mut ok := false
    inv := bigint.ModInverse(bigint.FromInt(3), bigint.FromInt(11), ok)
    println ok, bigint.ToStr(inv)
    println bigint.ToStr(bigint.Sqrt(bigint.Pow10(20)))
end
Output265252859812191058636308480000000 445 6 12 2 -9 47 true 4 10000000000

Gcd

func Gcd(a: Int, b: Int): Int
The greatest common divisor.

The example for this item is shown with the ones above: bigint-ModExp.salam

Lcm

func Lcm(a: Int, b: Int): Int
The least common multiple.

The example for this item is shown with the ones above: bigint-ModExp.salam

ExtGcd

func ExtGcd(a: Int, b: Int, x &: Int, y &: Int): Int
The extended greatest common divisor: besides the gcd it finds the coefficients x and y with a·x + b·y equal to the gcd.

The example for this item is shown with the ones above: bigint-ModExp.salam

ModInverse

func ModInverse(a: Int, m: Int, ok &: bool): Int
The modular inverse: the number whose product with a modulo m is one. When there is none, ok turns false.

The example for this item is shown with the ones above: bigint-ModExp.salam

Sqrt

func Sqrt(x: Int): Int
The integer square root: the largest number whose square is not greater than this one.

The example for this item is shown with the ones above: bigint-ModExp.salam

Factorial

func Factorial(n: int): Int
The factorial of n, with no size limit.

The example for this item is shown with the ones above: bigint-ModExp.salam

Neg

func Neg(x: Int): Int
The number with its sign reversed.

The example for this item is shown with the ones above: bigint-Add.salam

Abs

func Abs(x: Int): Int
The absolute value.

The example for this item is shown with the ones above: bigint-Add.salam

Add

func Add(a: Int, b: Int): Int
The sum of two numbers.

The example for this item is shown with the ones above: bigint-Add.salam

Sub

func Sub(a: Int, b: Int): Int
The difference of two numbers.

The example for this item is shown with the ones above: bigint-Add.salam

Mul

func Mul(a: Int, b: Int): Int
The product of two numbers.

The example for this item is shown with the ones above: bigint-Add.salam

Sqr

func Sqr(x: Int): Int
The number squared, which is faster than multiplying it by itself.

The example for this item is shown with the ones above: bigint-Add.salam

AddInt

func AddInt(a: Int, v: i64): Int
Adds an ordinary whole number.

The example for this item is shown with the ones above: bigint-Add.salam

SubInt

func SubInt(a: Int, v: i64): Int
Subtracts an ordinary whole number.

The example for this item is shown with the ones above: bigint-Add.salam

MulInt

func MulInt(a: Int, v: i64): Int
Multiplies by an ordinary whole number.

The example for this item is shown with the ones above: bigint-Add.salam

Shl

func Shl(x: Int, k: int): Int
Shifts left by k bits, that is, multiplies by 2 to the power k.

The example for this item is shown with the ones above: bigint-Add.salam

Shr

func Shr(x: Int, k: int): Int
Shifts right by k bits.

The example for this item is shown with the ones above: bigint-Add.salam

Set

func Set(x &: Int, src: Int)
Pours the value of src into x.

The example for this item is shown with the ones above: bigint-AddTo.salam

SetInt

func SetInt(x &: Int, v: i64)
Pours an ordinary whole number into x.

The example for this item is shown with the ones above: bigint-AddTo.salam

AddTo

func AddTo(x &: Int, y: Int)
Adds y to x, changing x itself.

The example for this item is shown with the ones above: bigint-AddTo.salam

SubFrom

func SubFrom(x &: Int, y: Int)
Subtracts y from x.

The example for this item is shown with the ones above: bigint-AddTo.salam

MulBy

func MulBy(x &: Int, y: Int)
Multiplies x by y.

The example for this item is shown with the ones above: bigint-AddTo.salam

AddIntTo

func AddIntTo(x &: Int, v: i64)
Adds an ordinary whole number to x.

The example for this item is shown with the ones above: bigint-AddTo.salam

MulIntBy

func MulIntBy(x &: Int, v: i64)
Multiplies x by an ordinary whole number.

The example for this item is shown with the ones above: bigint-AddTo.salam

NegateIn

func NegateIn(x &: Int)
Reverses the sign of x.

The example for this item is shown with the ones above: bigint-AddTo.salam

ShlBy

func ShlBy(x &: Int, k: int)
Shifts x left in place.

The example for this item is shown with the ones above: bigint-AddTo.salam

ShrBy

func ShrBy(x &: Int, k: int)
Shifts x right in place.

The example for this item is shown with the ones above: bigint-AddTo.salam

DivMod

func DivMod(a: Int, b: Int, q &: Int, r &: Int): bool
Puts the quotient and the remainder into q and r together; the quotient is rounded towards zero. It returns false when the divisor is zero.
bigint-Div.salam
import bigint

func main:
    a := bigint.FromInt(-17)
    b := bigint.FromInt(5)
    println bigint.ToStr(bigint.Div(a, b)), bigint.ToStr(bigint.Rem(a, b))
    println bigint.ToStr(bigint.FloorDiv(a, b)), bigint.ToStr(bigint.FloorMod(a, b)), bigint.ToStr(bigint.Mod(a, b))
    mut q := bigint.Zero()
    mut r := bigint.Zero()
    println bigint.DivMod(a, b, q, r), bigint.ToStr(q), bigint.ToStr(r)
    println bigint.FloorDivMod(a, b, q, r), bigint.ToStr(q), bigint.ToStr(r)
    println bigint.DivMod(a, bigint.Zero(), q, r)
    mut rem := 0 as i64
    println bigint.ToStr(bigint.DivModInt(bigint.FromInt(100), 7, rem)), rem, bigint.ModInt(bigint.FromInt(100), 7)
end
Output-3 -2 -4 3 3 true -3 -2 true -4 3 false 14 2 2

Div

func Div(a: Int, b: Int): Int
The quotient, rounded towards zero, as in C.

The example for this item is shown with the ones above: bigint-Div.salam

Rem

func Rem(a: Int, b: Int): Int
The remainder, with the sign of the dividend.

The example for this item is shown with the ones above: bigint-Div.salam

FloorDivMod

func FloorDivMod(a: Int, b: Int, q &: Int, r &: Int): bool
Like DivMod, but the quotient is rounded down and the remainder takes the sign of the divisor, as in Python.

The example for this item is shown with the ones above: bigint-Div.salam

FloorDiv

func FloorDiv(a: Int, b: Int): Int
The quotient, rounded down.

The example for this item is shown with the ones above: bigint-Div.salam

FloorMod

func FloorMod(a: Int, b: Int): Int
The remainder of a floor division.

The example for this item is shown with the ones above: bigint-Div.salam

Mod

func Mod(a: Int, m: Int): Int
The non-negative remainder modulo m, for modular arithmetic.

The example for this item is shown with the ones above: bigint-Div.salam

DivModInt

func DivModInt(a: Int, v: i64, rem &: i64): Int
Division by an ordinary whole number; the remainder goes into rem.

The example for this item is shown with the ones above: bigint-Div.salam

ModInt

func ModInt(a: Int, v: i64): i64
The remainder of dividing by an ordinary whole number.

The example for this item is shown with the ones above: bigint-Div.salam