# Programming & values > 32 bits?

How do you go about doing math with values larger than 32 bits on a 32 bit system? (In C, for example).

It must be relatively trivial, as every little calculator program out there does it. But a google search didn’t reveal anything that I recognized as the answer. I understand that you have to use one variable as the high-order info and one as the low-order info - but how do you put the two together and output a single number?

That’s the part which eludes me. Thanks much!!

A lot of it is done with string operations. I think the phrase you’re looking for is “arbitrary precision arithmetic”.

Most big-integer libraries represent the numbers as strings and emulate the math operations in software. The ints can get as big as you want, but the math is quite slow.

I would imagine that a lot of calculator programs use the double-floating-point system, which can handle a wide range of values to at least 12 significant digits. Sure, there’s times when you need to do addition of 19-digit numbers without losing anything on the end, but I think most people don’t really care that much.

It is not actually hard to write your own arbitrary-precision arithmetic library (especially if you want arbitrary-size integers and not floating-point values), but it is generally a lot easier if you can download one of the many available prewritten libraries that handle that kind of thing. The GNU Scientific Library is a good example of this, if a bit overkill if you do not need any of its other features.

(As a nitpick: Full-precision floating point values on x86 chips are actually 80 bits wide.)

What do you mean by “output”?

If you mean “output to the screen or a file”, then you just print the high bits followed by the low bits, same as you would any number.

If you mean “output to another part of the program”, then that other part of the program would have to support whatever representation of large integers you have.

Earthworm Jim: If you want to limit yourself to 64-bit integers, it becomes a lot easier: In gcc (and, I think, all C compilers conforming to the most recent (1999) C standard) the integral types ‘long long’ and ‘unsigned long long’ are guaranteed to be at least 64 bits wide. The printf family of functions in recent glibc versions also know how to handle these double-wide integers if given the right formatting sequences, saving you from the eternal hassle of shift-and-mask.

(I now noticed that you specifically said ‘one variable for high-order info and one variable for low-order info’. This is essentially how ‘long long’ integers are handled on 32-bit x86 chips, but the compiler is more than capable of doing it for you if you know how to tell it to.)

Yeah, I was thinking about long longs specifically - I knew they existed, but I didn’t know how to output them nicely. I was using cygwin, and there’s nothing in the printf/fprintf/sprintf man page about “long longs”. Since reading your post I moved over to a redhat box; and lo & behold there’s “%lld” - just what I was looking for. (Now if I can only get my thousands separator to print correctly - GRR!)

And incidentally, the first hit I got when googling “arbitrary precision arithmetic” was “the GNU Multiple Precision Arithmetic Library, the fastest bignum library on the planet!”.

<sniff, sniff> You guys are the best!

I’m sure you must have been taught how to do long multiplication at some point. It’s basically a method of multiplying large numbers without knowing anything more than how to multiply and add the numbers less than 10. You can probably imagine how to do this while storing the working in a string. I doubt that computers use this algorithm, but it gives you an idea of how it is possible.

Oh, the gmp. I forgot about that, and I mentioned the gsl instead. Google saves us once again from my brain farts of doom.