← LibraryMultiprecision Integer RepresentationEngineering · MathematicsLesson 207/385← PrevNext →
ArticlePublished 7 Aug 20262 min readBy Kevin Joginmultiprecisionbignumarbitrary precisionrepresentation

Multiprecision Arithmetic

Multiprecision Integer Representation

How arbitrary-precision integers are stored, why the base is chosen to match the machine word, and the consequences for every algorithm above.

Engineering / MathematicsMultiprecision Arithmetic2 min readKV-MATH-0505

Every algorithm in this collection operates on integers far larger than a machine word. The representation chosen for those integers sets the cost of every operation performed on them, so it is worth understanding before anything else.

Positional representation in base B

A multiprecision integer is stored as a sign together with an array of digits in some base B, least significant first. The digits are conventionally called limbs or words.

x = sign sum from i=0 to k-1 of a_i B^i, 0 <= a_i < BThe a_i are the limbs; k is the length.

Choosing the base

The base is chosen to be a power of two matching the machine word — typically B = 2^32 or B = 2^64. Two reasons dominate.

Hardware alignment

Addition, multiplication and division of single limbs map directly onto machine instructions. A non-power-of-two base would require explicit reduction after every operation.

Carry handling

With a power-of-two base, extracting the carry from a sum is a shift and a mask rather than a division.

Normalisation

Two invariants are maintained by every well-behaved implementation, and violating either produces bugs that surface far from their cause.

Cost consequences

Costs relative to limb count k
OperationCost in limbsNotes
Addition, subtractionO(k)Linear, carry propagation only
ComparisonO(k) worst caseLength check first, usually O(1)
MultiplicationO(k^2) schoolbookSee Karatsuba
DivisionO(k^2)Larger constant than multiplication
Shift by whole limbsO(k) or freeOften just an offset

Frequently Asked Questions

Should I write my own multiprecision library?
Almost never. The arithmetic is subtle, the performance gap against a mature library is large, and the bugs are hard to find. Write one only as a learning exercise.
Why store limbs least significant first?
Because carries propagate upward. Storing them in that order lets addition and multiplication walk the array forward, which is friendlier to memory prefetching and simplifies growing the array.

Source. Henri Cohen, A Course in Computational Algebraic Number Theory, Springer GTM 138 — 1.2.1. Structural reference unverified: the source file was not available during authoring; chapter and section numbers are taken from the published edition and have not been checked against a physical copy.

Continue learning

The Four Core Computational Tasks of Number FieldsArticle · MathematicsNEXT LESSON →Multiprecision Addition and SubtractionArticle · MathematicsLearning Pathways Through Computational Number TheoryArticle · MathematicsSchoolbook and Karatsuba MultiplicationArticle · Mathematics