Are there types bigger than long long int in C++?
My compiler is g++.
Are there types bigger than long long int in C++?
My compiler is g++.
There is a gcc extension for 128 bit integers.
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;
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
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 ;-)
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
you can check out BigInt class... http://sourceforge.net/projects/cpp-bigint/
(There are many other BigInts out there...)
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;
}