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

C++Types

C++ Problem Overview


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

My compiler is g++.

C++ Solutions


Solution 1 - C++

Solution 2 - C++

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

Solution 3 - C++

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;

Solution 4 - C++

__int128
__uint128

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

Solution 5 - C++

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

Solution 6 - C++

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 ;-)

Solution 7 - C++

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

Solution 8 - C++

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

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

Solution 9 - C++

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

Solution 10 - C++

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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelView Question on Stackoverflow
Solution 1 - C++JonView Answer on Stackoverflow
Solution 2 - C++jhoView Answer on Stackoverflow
Solution 3 - C++J TView Answer on Stackoverflow
Solution 4 - C++KitsuneYMGView Answer on Stackoverflow
Solution 5 - C++Ciro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 6 - C++Spike0xffView Answer on Stackoverflow
Solution 7 - C++Gunner StoneView Answer on Stackoverflow
Solution 8 - C++TCSView Answer on Stackoverflow
Solution 9 - C++KapilView Answer on Stackoverflow
Solution 10 - C++cout Hello world endlView Answer on Stackoverflow