2

I'd like to write a deadsimple bignum class using a series of (unsigned) integers. I can loosely see how addition and subtraction would work, but division and multiplication is another story. I know 32-bit compilers can emuate 64-bit int's by splitting the int64 in two int32's. I'm looking for an efficient method to do that.

I'd like to have C++ code, not assembly. Speed is no primary concern, but the most efficient solution without assemble is always nice to have.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 3
    Loosely speaking: start with long multiplication and long division (which you learned when you were 6 or whatever) in base 2^16. This at least works, then optimize to taste (http://en.wikipedia.org/wiki/Multiplication_algorithm#Fast_multiplication_algorithms_for_large_inputs). – Steve Jessop Jan 05 '12 at 12:20
  • 2
    All C++ compilers support `long long`, which is at least 64bits. – Puppy Jan 05 '12 at 12:21
  • This is a requirement, rather than a question. What is your specific problem? Are you looking for example code? – Fred Foo Jan 05 '12 at 12:23
  • @DeadMG: There are a lot of C++ compilers out there, not all are particularly compliant, and `long long` isn't required until C++11. There is probably an 8-bit C++ compiler out there that doesn't bother with 64 bits. Still, I'd be interested to hear from @rubenvb what he's using… – Potatoswatter Jan 05 '12 at 12:25
  • 1
    I updated the question to more clearly state what I tried to ask. – rubenvb Jan 05 '12 at 12:42

6 Answers6

5

Perhaps this can serve as a starting point. It implements up to 2,048-bit unsigned integers, using a base-65,536 representation. This means each digit fits in 16 bits, and we can trivially detect overflow (even when multiplying) by simply using 32 bits for the results.

This is C code however, but should be trivial to port to C++, or just use as an inspiration. It's very much optimized for readability rather than speed since this is not exactly the kind of stuff I'm good at. :)

unwind
  • 391,730
  • 64
  • 469
  • 606
1

Purchase a hardback book called NUMERICAL RECIPES IN C++ and you will find what you are looking for on pages starting on 20.6 page p916 onto to p925

user80998
  • 48
  • 2
1

You'd best have a look at arbitrary precision arithmetic which will explain the thinking behind the process of emulating higher precision processors than then one that your code is running on.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35
0

What's wrong with just using long long? That's guaranteed to be at least 64 bits. Otherwise, your Knuth (vol. 2) should provide the basic algorithms.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

Use simple byte array. You can have size of integer whatever you want*. You just have to operate in binary and take endianess into consideration (your bits will be 7-6-5-4-3-2-1-0-15-14-13...)

*RAM is limited

friendzis
  • 799
  • 6
  • 17
0

for arbitrary sizes, give GMP a spin, it should also work for your 64bit math on 32bit if for some reason you can't rely on the compiler to do it for you.

Necrolis
  • 25,836
  • 3
  • 63
  • 101