Introduction
I study computer math by writing and refining my own BigInt library. So far, my first incarnation stores each digit of the base number 10 in successive elements of the vector. It can be multiplied and added with arbitrary precision. I want to speed it up using all the space available to me in the standard C ++ data type by converting to a 2 ^ x base.
Information
I am reading 1000 or more digits from stdin in base 10 and I want to convert them to base 2 ^ x so that I can easily store them in an array or vector of one of the standard C ++ data types, probably an unsigned int. I have only one idea how to do a basic transformation, re-division with the remainder method. Below is the C ++ code describing this method:
vector<int> digits; while(num!=0) { int mod = num%base; num = num/base; digits.push_back(mod); }
Puzzles
Some of the things that I have lost are whether division with remainder is the correct way to convert bases to large integers. I tried to see how the GMP library does it. gmp / mpn / generic / set_str.c is the corresponding c source file where the “magic” happens, but I'm not sure what happens there. Matt McCutchen BigInt seems to use re-division with the remainder method. If I use this method, I basically need to write two versions of my BigInt class, one for working in Base10, and the other for Base2 ^ x.
Conclusion
- We offer recommendations on the right steps to convert a huge number from a string into an array of 32-bit words.
- Help me find out how GMP converts a string into an array of 32-bit words, without wading through many levels of abstraction.
Examples Using 4-bit Word Size
The number we want to keep (obviously on a small size): 123456789
unsigned chars have a range of 0-255, if we want to split our number and store it in a vector, we can do this in one of three ways:
- As base 10, our vector is as follows: [1,2,3,4,5,6,7,8,9]
- This is what my vector looked like in my first implementation.
- As the base 100, our vector is as follows: [1,23,45,67,89]
- It is easy to convert from base 10 to base 100, it has ciel elements (numbers in base10 / 2).
- As a base 256, our vector is as follows: [7,91,205,21]
Obviously, the third solution is the most optimal for the internal representation and what I'm trying to get to.
source share