1

I need a way to use very very large data types. Whereas an int is typically 4 bytes, I need a data type of 536, 870, 912 bytes or maybe even higher. I can't just use an array because I need to be able to convert to string, add, subtract, multiply, divide, etc with them.

How can I create or use such a large data type? I'd need a uint4294967296_t :P

Note I have tried using structs with bitfields, but they aren't large enough and I can't convert between values using that.

Dylan Turner
  • 322
  • 1
  • 12
  • 4
    Use a [library](https://gmplib.org/), perhaps? – Greg Kikola Feb 19 '17 at 01:21
  • 1
    Greg is referring to the GIMP library, which is usually recommended for this. – Cheers and hth. - Alf Feb 19 '17 at 01:23
  • Voting to close because recommendations of libraries is currently off-topic on SO. – Cheers and hth. - Alf Feb 19 '17 at 01:24
  • 1
    You have several options, including GMP: https://gmplib.org/, or create your own class. This question has been asked several times on SO, including [here](http://stackoverflow.com/questions/310276/how-to-handle-arbitrarily-large-integers) and [here](http://stackoverflow.com/questions/28685799/arbitrary-size-integers-in-c-c). – paulsm4 Feb 19 '17 at 01:25
  • If you want to perform the multi-precision math yourself, then I suggest you take a look at Donald Knuth's [Art of Computer Programming](https://en.wikipedia.org/wiki/The_Art_of_Computer_Programming). I believe Volume II, Seminumerical Algorithms, Chapter 4, Multiple Precision Arithmetic, is what you are interested in. Also see [How to add 2 arbitrarily sized integers in C++?](https://stackoverflow.com/q/2926219/608639), which provides code for some C++ libraries and OpenSSL. – jww Aug 29 '17 at 17:49

1 Answers1

2

Use a Bignumber library, I prefer TTMath for its simplicity. You can find it here Link to tttmath. TTTmath allows for operation on large numbers but You may need to make your own toString method.

Here is an example of TTTmath in use from their Samples page:

#include <ttmath/ttmath.h>
#include <iostream>

int main()
{
ttmath::UInt<2> a,b,c;

    a = "1234";
    b = 3456; 
    c = a*b;

    std::cout << c << std::endl;
}
Listing nr 1
  • Recommendation of a library should be a comment, not an answer. Flagged this as such. (That means, hopefully a mod will convert it to a comment.) – Cheers and hth. - Alf Feb 19 '17 at 01:31
  • 4
    Why? He didn't ask how to implement big numbers, but how to use them. So a library recommendation should be a valid answer imo. – zett42 Feb 19 '17 at 01:36
  • Questions asking for library recommendations are off-topic on SO (it's one of the stock close reasons). – Cheers and hth. - Alf Feb 19 '17 at 01:41
  • 3
    Seems to be a bit nitpicky to me if a library is the only sane way to solve the problem at hand. And he actually didn't ask for a library recommendation but described his problem which would be in line with http://stackoverflow.com/help/on-topic – zett42 Feb 19 '17 at 02:10
  • @zett42: I agree it would be useful with these kinds of questions on SO, but such are the current rules. I think because it's always possible to add new different answers, an arbitrarily long sequence of recommendations. E.g. the C++ book list FAQ, a really useful question, suffered from this problem and was converted to a less useful format. – Cheers and hth. - Alf Feb 19 '17 at 02:58
  • I will include an implementation so that I meet the community standards. Thanks for the heads up. –  Feb 20 '17 at 20:56