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?

3
  • 1
    I've never seen a 128-bit unsigned long long. You could always use a library that represents big numbers. Commented Sep 4, 2012 at 21:38
  • 3
    struct { uint64_t lo, hi; }? Commented Sep 4, 2012 at 21:39
  • 1
    On x86_64, GCC can generate a synthetic two-word type for you. Check if __uint128_t doesn't already exist. Commented Sep 4, 2012 at 21:46

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Can it [efficiently] handle 128bit integers? (That is, not arbitrary precision cases.)
@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?
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.
@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 ints.
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/

Comments

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.

Comments

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.

1 Comment

Above mentioned link is not working. Can you point me to the correct link please?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.