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

前端 未结 9 751
遥遥无期
遥遥无期 2020-12-08 13:18

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

My compiler is g++.

相关标签:
9条回答
  • 2020-12-08 13:30

    There is a gcc extension for 128 bit integers.

    0 讨论(0)
  • 2020-12-08 13:30

    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

    0 讨论(0)
  • 2020-12-08 13:33

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

    0 讨论(0)
  • 2020-12-08 13:36

    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;
    
    0 讨论(0)
  • 2020-12-08 13:38

    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

    0 讨论(0)
  • 2020-12-08 13:49

    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

    0 讨论(0)
提交回复
热议问题