2

Possible Duplicate:
Representing 128-bit numbers in C++

I need a way to store a 128 bit number, is there something besides unsigned long long that I can use?

Community
  • 1
  • 1
user1647008
  • 343
  • 1
  • 3
  • 8

4 Answers4

2

You may want to use the GNU Multiple Precision Arithmetic Library.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • Can it [efficiently] handle 128bit integers? (That is, not arbitrary precision cases.) –  Sep 04 '12 at 21:41
  • @pst: I don't know whether it has any optimizations for fixed-size integers, but I believe it is quite performant anyway. How about writing a small benchmark that resembles your situation and compares e.g. addition in GMP to a hand-written addition of pairs of 64-bit integers? – Aasmund Eldhuset Sep 04 '12 at 21:47
  • I've never used GMP (although it does sound like a good approach, especially if anything besides "storing" the value is required). I was seeking any extra off-hand insights into its usage for such scenarios. Such little tidbits can really "flush out" a question. –  Sep 04 '12 at 22:12
  • @pst: I've only used GMP once (for some calculations in my master's thesis), but I found it very convenient to work with. In C++, it overloads operators so that you can work with arbitrary-precision integers almost like regular `int`s. – Aasmund Eldhuset Sep 05 '12 at 07:32
1

There's no primitive type for that.
Vlad's comment is a good solution for storage, but if you need to use that number for computations, you'll need to use a library allowing representation and arithmetic operations on big numbers.

You should start by taking a look at GMP:

http://gmplib.org/

Macmade
  • 52,708
  • 13
  • 106
  • 123
0

If you only need to store it then you can store it in a byte array like "char num128[16]".

If you need to manipulate it you need to use big numbers library like GMP.

Richard Heath
  • 349
  • 3
  • 12
0

It is not possible to store it in one primitive data type, so we have to be slightly more creative. Probably the easiest way to do it is to have the class hold two 64-bit ints, representing the upper and lower halves of the integer.

heretolearn
  • 6,387
  • 4
  • 30
  • 53