60

Are there types bigger than long long int in C++?

My compiler is g++.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Michael
  • 609
  • 1
  • 5
  • 3
  • 2
    What types are you talking about? Integer types or any types? What's "bigger" in this case: range or size? In any case, type `int[100]` is most likely a lot bigger than `long long`, but I don't think this is what you are asking about. Clarify your question. At this time it makes little sense. – AnT stands with Russia Mar 21 '11 at 18:25
  • 2
    you could try long long long (I'm just kidding) I believe long long is the biggest at 64 bits on all architectures – Jesus Ramos Mar 21 '11 at 18:25
  • 2
    @Jesus Ramos: With at least 64 bits (could be bigger). @AndreyT: Not officially, but it is a nearly ubiquitous extension that we also expect to see in the next version of the standard (to bring us into line with C). – Martin York Mar 21 '11 at 19:04
  • 2
    The thing is that sizeof(long long) has a loose definition being sizeof(long) <= sizeof(long long) so on certain machines they are actually the same size 0_0 odd aint it – Jesus Ramos Mar 21 '11 at 19:05
  • `__m128` in my compiler, not standard of course. – Hans Passant Mar 21 '11 at 19:40
  • 4
    where do I click when a comment is technically correct but excessively nitpicky... @AndreyT seriously, you didn't understand what he meant? – Spike0xff Jan 18 '13 at 18:12
  • @Spike0xff: No, I didn't. While the `int[100]` was obviously an intentional exaggeration (as I said right away), there's still no way to figure out whether, say, `double` can qualify as "bigger" type for OP's purposes. Is it? I don't know. It has "wider" absolute range, but it comes with trade-offs, since it has the same number of bytes (assuming both are 64 bit). So, is it "bigger" or not? If this is perfectly clear to you, then please explain it to me. – AnT stands with Russia Jan 18 '13 at 19:05
  • **unsigned long long** – anilbey Aug 27 '14 at 15:56
  • You might check out this [wikipedia article](http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic), which links to a number of libraries that can handle larger-than-long-long storage. – paulcam Mar 21 '11 at 18:27

10 Answers10

24

There is a gcc extension for 128 bit integers.

Jon
  • 428,835
  • 81
  • 738
  • 806
12

No, but you can use libraries like GMP to handle bigger numbers.

jho
  • 2,243
  • 1
  • 15
  • 10
12

Depending on what your need is, you could create your own struct to handle the data type:

#include <cstdint>

struct uint256_t
{
    std::uint64_t bits[4];
};

uint256_t x;
J T
  • 4,946
  • 5
  • 28
  • 38
9

__int128
__uint128

​​​​​ ​​​​​​​​​​​​​​​

KitsuneYMG
  • 12,753
  • 4
  • 37
  • 58
8

Standards

Extended integer types are explicitly allowed by the C and C++ standards.

C++11

C++11 N3337 draft 3.9.1 "Fundamental types" paragraph 3 says:

There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. Plain ints have the natural size suggested by the architecture of the execution environment the other signed integer types are provided to meet special needs.

You should also consider intmax_t, which 18.4.1 "Header synopsis" paragraph 2 says:

The header defines all functions, types, and macros the same as 7.18 in the C standard.

C99

C99 N1256 draft explicitly allows them at 6.2.5 "Types" paragraph 4:

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) There may also be implementation-defined extended signed integer types.28) The standard and extended signed integer types are collectively called signed integer types.29)

and 7.18.1.5 "Greatest-width integer types" paragraph 1 says:

The following type designates a signed integer type capable of representing any value of any signed integer type:

intmax_t

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
7

Summarizing...

If you need to store exact integer values that won't fit in 'long long', gcc offers the type __int128. This is a gcc extension, not part of standard C++ (as of this writing).

If you need to work with even bigger exact integer values, you probably need an arbitrary-precision arithmetic package, such as GMP. If your need is very limited you could roll your own extended precision code, but that can quickly become more complicated (and less efficient and reliable) than using an existing library.

If you need to store larger numbers but don't need to store the larger values exactly, you can use float or double: These can represent numbers of much larger magnitude, but with less precision.

And of course, if you just want to take up more memory, declare an array ;-)

Spike0xff
  • 1,246
  • 1
  • 15
  • 24
  • Could one possibility be to use `streampos`, depending on the size needed? – jorgen Feb 05 '14 at 19:10
  • @jorgen streampos is probably not a help in general, as C++ 11 says "It is a typdef of one the fundamental signed integral types..." – Spike0xff Sep 13 '19 at 16:16
4

If you know your number is always going to be positive, you can extend the scope of an int by labeling it as unsigned

int myNum; // Range is from –2,147,483,648 to 2,147,483,647

unsigned int myNum; // Range is from 0 to 4,294,967,295

Gunner Stone
  • 997
  • 8
  • 26
2

you can check out BigInt class... http://sourceforge.net/projects/cpp-bigint/

(There are many other BigInts out there...)

TCS
  • 5,790
  • 5
  • 54
  • 86
2

In g++, there is a __int128 in cstdint header.

__int128 is 128 bit data type. Range is from -2^128 to 2^128-1. You can use __int128 with

#include <cstdint>

int main(){
    __int128 bignumber;
}
1

You can use

#include <boost/multiprecision/cpp_int.hpp>  
using namespace boost::multiprecision;

to work with data type bigger than long long int and the data type is cpp_int Ref

Kapil
  • 362
  • 6
  • 7