Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Construction

From zero

var x = BInt()          # 0

From Int

var x = BInt(42)
var y = BInt(-100)
var z: BInt = 42        # Implicit conversion from Int

The constructor is marked @implicit, so Mojo can automatically convert Int to BInt where expected.

From an integer literal of any size

var a: BInt = 12345678901234567890123456789012345678901234567890
var b: BInt = -340282366920938463463374607431768211457
var c = BInt(2**200)
var d = a + 99999999999999999999999  # works wherever a BInt is expected

A literal wider than Int can be written directly, with no string. Its words are worked out at compile time, so nothing is parsed at run time and nothing can fail. BUInt accepts literals the same way; a negative literal is a compile-time error there.

From String

var a = BInt("12345678901234567890")  # Basic decimal string
var b = BInt("-98765")                 # Negative number
var c = BInt("1_000_000")             # Underscores as separators
var d = BInt("1,234,567")             # Commas as separators
var e = BInt("1.23e5")                # Scientific notation (= 123000)
var f = BInt("1991_10,18")            # Mixed separators (= 19911018)

Note

The string must represent an integer. BInt("1.5") raises an error. Scientific notation like "1.23e5" is accepted only if the result is an integer.

From Scalar (any integral SIMD type)

var x = BInt(UInt32(42))
var y = BInt(Int64(-5))
var z: BInt = UInt32(99)     # Implicit conversion

Accepts any integral scalar type (Int8 through Int256, UInt8 through UInt256, etc.).

Summary of constructors

ConstructorDescription
BInt()Zero
BInt(value: IntLiteral)From a literal of any size (implicit)
BInt(value: String)From decimal string (raises)
BInt(value: Scalar)From any integral scalar (implicit)
BInt.from_integral_scalar(value)From any integral scalar type
BInt.from_string(value)Explicit factory from string (raises)
BInt.from_biguint(value, sign)From a base-10^18 magnitude and a sign

Unsafe constructors

These constructors skip validation for performance-sensitive code. The caller must ensure the data is valid.

ConstructorDescription
BInt(uninitialized_capacity=n)Empty words list with reserved capacity
BInt(raw_words=Magnitude, sign=Bool)From raw words, no leading-zero stripping

Magnitude is BInt’s word storage, exported from decimo. Its words are UInt64 and its base is 2^64; before v0.14.0 this was a List[UInt32] in base 2^32, so a literal is now [UInt64(1)], and an existing List goes in as BInt(raw_words=Magnitude(words^), sign=False).