A Bigint Calculator

Mon, 09 May 2016 00:00:00 GMT

This is a programming exercise I've wanted to do for a long time: an implementation of arbitrary-precision integer ("bigint") arithmetic in C.

Computers have instructions for doing arithmetic on binary numbers of certain sizes, most commonly 32 or 64 bits. The largest 32-bit number is 2^32-1, or 4,294,967,295; computations on larger numbers have to be performed in multiple steps operating on smaller pieces.

This is similar to how humans compute: we can handle numbers up to a certain size in our heads, but for larger numbers, we use methods that break the computation into smaller steps. For example, to calculate 5678 + 9012, we might use pen and paper to add one decimal position at a time from right to left, "carrying the one" as necessary.

Those same methods, or algorithms, we learned at school are used by computers as well (and computers are usually better at executing them than we are). This post shows implementations of these algorithms, and how they can be used to build a simple calculator.