bigint
Whole numbers of unlimited size: large factorials, cryptography, and any arithmetic that will not fit in 64 bits.
import bigint
An i64 stops at about 9.2 × 1018 and wraps around without warning. bigint.Int is a whole number whose size is limited only by memory: use it for factorials and combinatorics, exact counters that can grow without bound, token amounts in their smallest unit, and number theory such as modular exponentiation. For money with a fixed number of decimal places use decimal, and for real keys and signatures use crypto.
A number is a sign and a vector of 32-bit limbs. Multiplication is the schoolbook method and conversion to text divides repeatedly, so numbers of a few thousand digits are quick and numbers of a million digits are slow.
Most functions return a new number and leave their arguments alone. The ones whose names end in To, From or By, such as AddTo, store the result in their first argument and free its old value for you. They still allocate the new value, so they are not faster; what they save is the bookkeeping of freeing every intermediate result. Release a number you no longer need with Free.
What people get wrong:
- Copying with
=.b := acopies only the header, so both names share one set of limbs, and the next in-place call onbfrees the memoryastill points to. UseClonefor a second, independent number. - Dividing by zero.
Div,Rem,ModandModExpdo not stop the program; they return 0. When the divisor can be zero, useDivMod, which returnsfalse. - Expecting an error for impossible input. A negative exponent in
Pow, the square root of a negative number and the factorial of a negative number all return 0, andToStrBasewith a base outside 2 to 36 returns empty text.
Past the end of i64
The 93rd Fibonacci number is the first one that does not fit in an i64; with ordinary integers it would wrap around to a negative value without warning. With bigint.Int the loop just keeps going. ToInt converts back and tells you, through its bool argument, whether the value fitted, so you can move to plain integers when the numbers are small again.
import bigint
import str
func main:
mut a := bigint.Zero()
mut b := bigint.One()
repeat 1 to 300 in n:
next := bigint.Add(a, b)
bigint.Free(a)
a = b
b = next
if n == 92 or n == 93:
mut fits := false
v := bigint.ToInt(a, fits)
println "F(" + str.FromInt(n) + ") =", bigint.ToStr(a), "fits in i64:", fits, v
end
end
text := bigint.ToStr(a)
println "F(300) has", text.Len(), "digits"
println text
bigint.Free(a)
bigint.Free(b)
endEach Add returns a new number, so the loop frees the value it no longer needs before it moves the names along. Without the Free calls the program would still be correct, but it would keep every intermediate number in memory.
Updating one number in a loop
A loop that updates one number with Add has to free the old value on every step, as the Fibonacci loop above does. MulIntBy, AddTo, ShlBy and the other functions named with To, From or By do that for you: they put the result into their first argument and free what was there. Build the result in one variable and free it once at the end.
import bigint
func main:
mut product := bigint.One()
defer bigint.Free(product)
repeat 1 to 50 in i:
bigint.MulIntBy(product, i as i64)
end
mut check := bigint.Factorial(50)
defer bigint.Free(check)
println "50! =", bigint.ToStr(product)
println "same as Factorial(50):", bigint.Equals(product, check)
mut total := bigint.Zero()
defer bigint.Free(total)
mut power := bigint.One()
defer bigint.Free(power)
repeat 64:
bigint.AddTo(total, power)
bigint.ShlBy(power, 1)
end
println "1 + 2 + 4 + ... + 2^63 =", bigint.ToStr(total)
println "that is 2^64 - 1:", bigint.Equals(total, bigint.SubInt(power, 1))
endCopies and snapshots
To keep an old value while you change the number, take a copy with Clone. A plain opening := balance would not work: both names would share the same limbs, and the first AddIntTo would free them under opening, which then prints garbage. Each clone is its own allocation and needs its own Free.
import bigint
func main:
mut ok := false
mut balance := bigint.Parse("900000000000000000000", ok)
mut opening := bigint.Clone(balance)
defer bigint.Free(balance)
defer bigint.Free(opening)
bigint.AddIntTo(balance, 250 as i64)
bigint.MulIntBy(balance, 2 as i64)
println "opening:", bigint.ToStr(opening)
println "now: ", bigint.ToStr(balance)
mut gain := bigint.Sub(balance, opening)
defer bigint.Free(gain)
println "gain: ", bigint.ToStr(gain)
endDigits of very large numbers
ToStr gives the decimal text, so its length is the number of digits. DivModInt divides by an ordinary integer and puts the remainder into an i64, which is the way to walk through the digits one by one, here to add them up. TrailingZeros counts zero bits, not decimal zeros.
import bigint
func digit_sum(x: bigint.Int): i64:
mut rest := bigint.Clone(x)
defer bigint.Free(rest)
mut sum := 0 as i64
until bigint.IsZero(rest) == false:
mut d := 0 as i64
q := bigint.DivModInt(rest, 10, d)
bigint.Free(rest)
rest = q
sum += d
end
ret sum
end
func main:
mut p := bigint.Pow(bigint.FromInt(2), 1000)
mut f := bigint.Factorial(100)
defer bigint.Free(p)
defer bigint.Free(f)
println "2^1000 has", bigint.ToStr(p).Len(), "digits, digit sum", digit_sum(p)
println "100! has", bigint.ToStr(f).Len(), "digits, digit sum", digit_sum(f)
println "100! ends in", bigint.TrailingZeros(f), "zero bits"
endReading and printing in other bases
Parse reads decimal text and reports through its bool argument whether the text was a whole number; an empty string, a stray letter or a trailing space fails. Leading spaces, a sign, Persian digits and _ between digits (1_000_000) are accepted. With base 0, ParseBase reads a 0x, 0b or 0o prefix and falls back to decimal. ParseBase and ToStrBase handle bases from 2 to 36, and ToHex is the usual form for keys and hashes. ToIntClamped gives the largest or smallest i64 when the value is out of range.
import bigint
func main:
inputs := ["340282366920938463463374607431768211455", "-12345678901234567890", "12a4", ""]
each s in inputs:
mut ok := false
mut n := bigint.Parse(s, ok)
if ok:
println s, "->", bigint.BitLen(n), "bits, sign", bigint.Sign(n)
else:
println "[" + s + "] is not a whole number"
end
bigint.Free(n)
end
mut ok := false
mut key := bigint.ParseBase("deadbeefcafebabe0123456789", 16, ok)
println ok, bigint.ToStr(key)
println "base 36:", bigint.ToStrBase(key, 36)
println "back to hex:", bigint.ToHex(key)
println "clamped to i64:", bigint.ToIntClamped(key)
bigint.Free(key)
endDivision, remainders and zero
There are three ways to divide, and they differ only for negative numbers. Div and Rem round towards zero, as C and Go do. FloorDiv and FloorMod round down, as Python does, so the remainder takes the sign of the divisor. Mod and ModInt always give a remainder from 0 up to the divisor, which is what modular arithmetic and a day-of-week calculation want. DivMod gives the quotient and the remainder from one division, and its bool is the only way to notice a zero divisor.
import bigint
func show(label: str, x: bigint.Int):
println label, bigint.ToStr(x)
end
func main:
mut ok := false
mut pool := bigint.Parse("1_250_000_000_000_000_000_000", ok)
defer bigint.Free(pool)
mut winners := bigint.FromInt(7)
defer bigint.Free(winners)
mut share := bigint.Zero()
mut dust := bigint.Zero()
defer bigint.Free(share)
defer bigint.Free(dust)
if bigint.DivMod(pool, winners, share, dust):
show("each winner gets:", share)
show("left in the pool:", dust)
end
mut nobody := bigint.Zero()
defer bigint.Free(nobody)
if bigint.DivMod(pool, nobody, share, dust) == false:
println "no winners: DivMod returned false"
end
mut silent := bigint.Div(pool, nobody)
defer bigint.Free(silent)
show("Div by zero quietly gives", silent)
mut debt := bigint.FromInt(-10)
mut days := bigint.FromInt(3)
defer bigint.Free(debt)
defer bigint.Free(days)
mut t := bigint.Div(debt, days)
mut m := bigint.Rem(debt, days)
println "Div, Rem:", bigint.ToStr(t), bigint.ToStr(m)
bigint.Free(t)
bigint.Free(m)
t = bigint.FloorDiv(debt, days)
m = bigint.FloorMod(debt, days)
println "FloorDiv, FloorMod:", bigint.ToStr(t), bigint.ToStr(m)
bigint.Free(t)
bigint.Free(m)
println "ModInt:", bigint.ModInt(debt, 3)
endModular arithmetic: a toy RSA
Public key cryptography is built on ModExp, ModInverse and Gcd. The program below makes an RSA key from two small primes, encrypts a number with the public exponent and decrypts it with the private one. ModInverse fails, through its bool, when the two numbers share a factor.
import bigint
func main:
p := bigint.FromInt(1000000007)
q := bigint.FromInt(998244353)
n := bigint.Mul(p, q)
phi := bigint.Mul(bigint.SubInt(p, 1), bigint.SubInt(q, 1))
e := bigint.FromInt(65537)
println "public key n:", bigint.ToStr(n), "e:", bigint.ToStr(e)
println "gcd(e, phi):", bigint.ToStr(bigint.Gcd(e, phi))
mut ok := false
d := bigint.ModInverse(e, phi, ok)
if not ok:
println "e has no inverse, pick another e"
ret 1
end
println "private exponent d:", bigint.ToStr(d)
message := bigint.FromInt(424242424242)
secret := bigint.ModExp(message, e, n)
back := bigint.ModExp(secret, d, n)
println "message: ", bigint.ToStr(message)
println "encrypted:", bigint.ToStr(secret)
println "decrypted:", bigint.ToStr(back)
println "round trip:", bigint.Equals(message, back)
ret 0
endThis is for learning how the arithmetic works. Real RSA needs primes of more than a thousand bits, random padding and constant time code; for real keys and signatures use crypto.
| Job | Use |
|---|---|
| Arithmetic that returns a new number | Add, Sub, Mul, Div, Pow |
| A long loop that updates one number | AddTo, MulBy, MulIntBy, ShlBy |
| A second, independent number | Clone |
| Division that can see a zero divisor | DivMod, FloorDivMod |
| Comparisons | Cmp, Equals, Less, Greater |
| Modular arithmetic | ModExp, ModInverse, Mod |
| Text in and out | Parse, ParseBase, ToStr, ToHex |
| Back to a normal integer | ToInt, ToIntClamped |
Types
Int
struct IntFromInt or Parse. Assigning it to another name shares its limbs; use Clone for a copy.| Name | Type | Description |
|---|---|---|
neg | bool = false | True when the number is negative. |
mag | Vector | The absolute value, as 32-bit limbs from least to most significant. |
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.ToStr()
println a.is_zero(), a.sign(), a.is_odd(), a.is_even()
println b.bit_len(), b.limb_count()
c := a.Clone()
println c.ToStr()
endFree
func Free()free
func free()The example for this item is shown with the ones above: bigint-Int.salam
IsZero
func IsZero(): boolis_zero
func is_zero(): boolThe example for this item is shown with the ones above: bigint-Int.salam
Sign
func Sign(): intsign
func sign(): intThe example for this item is shown with the ones above: bigint-Int.salam
IsOdd
func IsOdd(): boolis_odd
func is_odd(): boolThe example for this item is shown with the ones above: bigint-Int.salam
IsEven
func IsEven(): boolis_even
func is_even(): boolThe example for this item is shown with the ones above: bigint-Int.salam
BitLen
func BitLen(): intbit_len
func bit_len(): intThe example for this item is shown with the ones above: bigint-Int.salam
LimbCount
func LimbCount(): intlimb_count
func limb_count(): intThe example for this item is shown with the ones above: bigint-Int.salam
Clone
func Clone(): Intclone
func clone(): IntThe example for this item is shown with the ones above: bigint-Int.salam
ToStr
func ToStr(): strto_str
func to_str(): strThe example for this item is shown with the ones above: bigint-Int.salam
Functions
ToStr
func ToStr(x: Int): strimport 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))
endToStrBase
func ToStrBase(x: Int, base: int): str- for a negative number. Another base gives empty text.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)
endToHex
func ToHex(x: Int): str0x prefix.The example for this item is shown with the ones above: bigint-Parse.salam
Parse
func Parse(s: str, ok &: bool): Int_ between digits are accepted. When the text is not a valid number, ok turns false and the result is 0.The example for this item is shown with the ones above: bigint-Parse.salam
ParseBase
func ParseBase(s: str, base: int, ok &: bool): Int0x, 0b or 0o prefix and otherwise means base 10.The example for this item is shown with the ones above: bigint-Parse.salam
ParseOr
func ParseOr(s: str, base: int): IntThe example for this item is shown with the ones above: bigint-Parse.salam
ToInt
func ToInt(x: Int, ok &: bool): i64i64; when it does not fit, ok turns false and the result is 0.The example for this item is shown with the ones above: bigint-Parse.salam
ToIntClamped
func ToIntClamped(x: Int): i64i64, 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): f64f64; 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): intimport 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)
endFromBytes
func FromBytes(p: void*, n: int): Intn 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): booln 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
Zero
func Zero(): IntThe example for this item is shown with the ones above: bigint-Add.salam
One
func One(): IntThe example for this item is shown with the ones above: bigint-Add.salam
FromInt
func FromInt(v: i64): Inti64.The example for this item is shown with the ones above: bigint-Int.salam
FromUint
func FromUint(v: u64): Intu64.The example for this item is shown with the ones above: bigint-Add.salam
Clone
func Clone(x: Int): Intimport 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)
endFree
func Free(x &: Int)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)
endIsZero
func IsZero(x: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Sign
func Sign(x: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
BitLen
func BitLen(x: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
Bit
func Bit(x: Int, i: int): inti 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): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
IsEven
func IsEven(x: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Cmp
func Cmp(a: Int, b: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
CmpAbs
func CmpAbs(a: Int, b: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
Equals
func Equals(a: Int, b: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Less
func Less(a: Int, b: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Greater
func Greater(a: Int, b: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Min
func Min(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Cmp.salam
Max
func Max(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Cmp.salam
EqualsInt
func EqualsInt(a: Int, v: i64): booli64.The example for this item is shown with the ones above: bigint-Cmp.salam
Neg
func Neg(x: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Abs
func Abs(x: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Add
func Add(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Sub
func Sub(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Mul
func Mul(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Sqr
func Sqr(x: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
AddInt
func AddInt(a: Int, v: i64): IntThe example for this item is shown with the ones above: bigint-Add.salam
SubInt
func SubInt(a: Int, v: i64): IntThe example for this item is shown with the ones above: bigint-Add.salam
MulInt
func MulInt(a: Int, v: i64): IntThe example for this item is shown with the ones above: bigint-Add.salam
Shl
func Shl(x: Int, k: int): Intk 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): Intk bits.The example for this item is shown with the ones above: bigint-Add.salam
Set
func Set(x &: Int, src: Int)x with a copy of src, freeing the old value.The example for this item is shown with the ones above: bigint-AddTo.salam
SetInt
func SetInt(x &: Int, v: i64)x with an ordinary whole number, freeing the old value.The example for this item is shown with the ones above: bigint-AddTo.salam
AddTo
func AddTo(x &: Int, y: Int)y to x: the sum replaces x and the old value is freed.The example for this item is shown with the ones above: bigint-AddTo.salam
SubFrom
func SubFrom(x &: Int, y: Int)y from x.The example for this item is shown with the ones above: bigint-AddTo.salam
MulBy
func MulBy(x &: Int, y: Int)x by y.The example for this item is shown with the ones above: bigint-AddTo.salam
AddIntTo
func AddIntTo(x &: Int, v: i64)x.The example for this item is shown with the ones above: bigint-AddTo.salam
MulIntBy
func MulIntBy(x &: Int, v: i64)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)x.The example for this item is shown with the ones above: bigint-AddTo.salam
ShlBy
func ShlBy(x &: Int, k: int)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)x right in place.The example for this item is shown with the ones above: bigint-AddTo.salam
Pow
func Pow(x: Int, e: int): Inte, by repeated squaring. A negative e gives 0.The example for this item is shown with the ones above: bigint-Int.salam
Pow10
func Pow10(n: int): Intn.The example for this item is shown with the ones above: bigint-Add.salam
ModExp
func ModExp(base: Int, e: Int, m: Int): Intbase to the power e modulo m without building the huge intermediate number; the basis of RSA. The result is from 0 to |m|-1. A zero m or a negative e gives 0.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)))
endGcd
func Gcd(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-ModExp.salam
Lcm
func Lcm(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-ModExp.salam
ExtGcd
func ExtGcd(a: Int, b: Int, x &: Int, y &: Int): Intx 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): Inta modulo m is one. When there is none, because a and m share a factor, ok turns false.The example for this item is shown with the ones above: bigint-ModExp.salam
Sqrt
func Sqrt(x: Int): IntThe example for this item is shown with the ones above: bigint-ModExp.salam
Factorial
func Factorial(n: int): Intn, with no size limit. A negative n gives 0.The example for this item is shown with the ones above: bigint-ModExp.salam
DivMod
func DivMod(a: Int, b: Int, q &: Int, r &: Int): boolq and r together; the quotient is rounded towards zero. It returns false when the divisor is zero.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)
endDiv
func Div(a: Int, b: Int): IntDivMod to detect it.The example for this item is shown with the ones above: bigint-Div.salam
Rem
func Rem(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Div.salam
FloorDivMod
func FloorDivMod(a: Int, b: Int, q &: Int, r &: Int): boolDivMod, 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): IntThe example for this item is shown with the ones above: bigint-Div.salam
FloorMod
func FloorMod(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Div.salam
Mod
func Mod(a: Int, m: Int): IntMod(-10, 3) is 2. A zero m gives 0.The example for this item is shown with the ones above: bigint-Div.salam
DivModInt
func DivModInt(a: Int, v: i64, rem &: i64): Intrem. A zero divisor gives 0 and a remainder of 0.The example for this item is shown with the ones above: bigint-Div.salam
ModInt
func ModInt(a: Int, v: i64): i64Mod.The example for this item is shown with the ones above: bigint-Div.salam
And
func And(a: Int, b: Int): Intimport 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)
endOr
func Or(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
Xor
func Xor(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
AndNot
func AndNot(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
Not
func Not(x: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
PopCount
func PopCount(x: Int): intThe example for this item is shown with the ones above: bigint-And.salam
TrailingZeros
func TrailingZeros(x: Int): intThe example for this item is shown with the ones above: bigint-And.salam