C ++中最大的整数数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1417113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:53:25  来源:igfitidea点击:

biggest integer datatype in c++?

c++

提问by jalf

Which is the biggest integer datatype in c++?

哪个是 C++ 中最大的整数数据类型?

回答by jalf

The biggest standardC++ integer type is long.

最大的标准C++ 整数类型是long.

C has a long long, and C++0x is going to add that as well, and of course you could implement your own custom integer type, perhaps even a BigInt class.

C 有一个long long, 而 C++0x 也将添加它,当然您可以实现自己的自定义整数类型,甚至可能是 BigInt 类。

But technically speaking, considering the built-in integer types, longis your answer.

但从技术上讲,考虑到内置的整数类型,long就是你的答案。

回答by Michael Koval

The long longdata-type is the largest built-in integral datatypes in standard C99 and C++0x. Just as with all of the other integral data types, long longis not given an exact size in bytes. Instead, it is defined to be at leasta 64-bit integer. While long longis not part of the official C++ standard, it is ubiquitously supported across modern compilers. Note, however, that many compilers for modern desktops define longand long longas both exactly 64-bits, whereas many compilers for embedded processors define longas 32-bits and long longas 64-bits (obviously witha few exceptions).

long long数据类型是最大的内置标准C99和C ++ 0x中不可或缺的数据类型。与所有其他整数数据类型一样,long long没有以字节为单位给出确切的大小。相反,它被定义为至少一个 64 位整数。虽然long long它不是官方 C++ 标准的一部分,但它在现代编译器中得到普遍支持。但是请注意,现代桌面的许多编译器都将long和定义long long为 64 位,而嵌入式处理器的许多编译器定义long为 32 位和long long64 位(显然有一些例外)。

If you need more precision or absolutely cannot use the long longlanguage extension, you're going to have to use one of the C or C++ libraries that are designed to work with extremely large or small numbers.

如果您需要更高的精度或绝对不能使用long long语言扩展,您将不得不使用旨在处理极大或极小数字的 C 或 C++ 库之一。

回答by underscore_d

You might prefer to avoid worrying about primitive names by mapping to the largest (fully realised) type on the compiling architecture via <cstdint>and its typedefs intmax_tand uintmax_t.

您可能更愿意通过映射到编译架构上的最大(完全实现)类型<cstdint>及其 typedefintmax_tuintmax_t.

I was surprised no one else said this, but cursory research indicates it was added in C++11, which can probably explain the lack of previous mentions. (Although its fellow new primitive/built-in type long longwascited!)

我很惊讶没有其他人这么说,但粗略的研究表明它是在 C++11 中添加的,这可能可以解释之前没有提到的原因。(虽然它的同胞新的原始/内置型long long引用!)

Some compilers may also provide larger types, though these may come with caveats, for instance: Why in g++ std::intmax_t is not a __int128_t?

一些编译器也可能提供更大的类型,尽管这些可能带有警告,例如:为什么在 g++ std::intmax_t 中不是 __int128_t?

Personally, I'm using cstdintas it's easier to quickly see how many bytes minimum I'm using - rather than having to remember how many bits a given primitive corresponds to - and the standard means it avoids my types being platform-dependent. Plus, for what I do, uint8_tis faster and neater than endless unsigned chars!

就我个人而言,我正在使用cstdint它,因为它更容易快速查看我使用的最少字节数 - 而不是必须记住给定原语对应的位数 - 并且标准意味着它避免了我的类型依赖于平台。另外,对于我所做的,uint8_t比无尽unsigned char的更快更整洁!

edit: In hindsight, I want to clarify: uint8_tis not guaranteed to be equivalent to unsigned char. Sure, it is on my machines, and it probably is for you, too. But this equivalance is not required by the Standard; see: When is uint8_t ≠ unsigned char?For that reason, now when I need the Standard-defined special abilities of [[un]signed] char, I use only that.

编辑:事后看来,我想澄清:uint8_t不能保证等同于unsigned char. 当然,它在我的机器上,也可能是给你的。但是标准并不要求这种等效性;请参阅:什么时候 uint8_t ≠ unsigned char?出于这个原因,现在当我需要 的标准定义的特殊能力时[[un]signed] char,我只使用它。

回答by Whatever

There are 128 bit packed integer and floating point formats defined in xmmintrin.h on compilers that support SSE to enable use of the SSE registers and instructions. They are not part of the C++ standard of course, but since they are supported by MSVC, GCC and the Intel C++ Compiler there is a degree of cross-platform support (at least OS X, Linux and Windows for Intel CPUs). Other ISAs have SIMD extensions so there are probably other platform/compiler specific extensions that support 128 or 256 bit SIMD instructions. Intel's upcoming AVX instruction set will have 256 bit registers, so we should see a new set of data types and intrinsics for that.

在支持 SSE 的编译器上的 xmmintrin.h 中定义了 128 位压缩整数和浮点格式,以启用 SSE 寄存器和指令的使用。它们当然不是 C++ 标准的一部分,但由于 MSVC、GCC 和英特尔 C++ 编译器支持它们,因此有一定程度的跨平台支持(对于英特尔 CPU 至少是 OS X、Linux 和 Windows)。其他 ISA 具有 SIMD 扩展,因此可能还有其他支持 128 或 256 位 SIMD 指令的平台/编译器特定扩展。英特尔即将推出的 AVX 指令集将有 256 位寄存器,因此我们应该会看到一组新的数据类型和内部函数。

They don't behave quite like built-in data types (i.e., you have to use intrinsic functions instead of operators to manipulate them, and they work with SIMD operations) but since they do in fact map to 128-bit registers on the hardware they deserve mention.

它们的行为不像内置数据类型(即,您必须使用内部函数而不是运算符来操作它们,并且它们与 SIMD 操作一起使用)但因为它们实际上映射到硬件上的 128 位寄存器他们值得一提。

Details about Streaming SIMD Extension (SSE)Intrinsics

有关流式 SIMD 扩展 (SSE) 内部函数的详细信息

回答by cdonat

boost::multiprecision::cpp_intis an arbitrary precision integer type. So there is no "biggest integer datatype" in C++. Just a biggest builtinintegral type, which AFAIK is longin standard C++.

boost::multiprecision::cpp_int是任意精度整数类型。所以在 C++ 中没有“最大的整数数据类型”。只是一个最大的内置整数类型,AFAIKlong在标准 C++ 中。

回答by Omar Al-Ithawi

You can easily get bigger datatype by defining your own Class. You can get inspired from the class BigIntegerin Java. It's a nice one but it's not necessarily an actual integer even if it acts exactly like one.

您可以通过定义自己的类轻松获得更大的数据类型。您可以从Java 中的BigInteger类中获得灵感。这是一个很好的整数,但它不一定是一个实际的整数,即使它的行为与 1 完全一样。

回答by David Andres

In the Borland and Microsoft compilers, __int64 is probably the largest you can get.

在 Borland 和 Microsoft 编译器中,__int64 可能是您可以获得的最大的。

回答by Hyman Giffin

The __int128_t and __uint128_t (unsigned __int128_t) data types are 128 bits long, double the size of a long long (which is 64 bits long for those new to c++). However, if you are going to use them, then you need to do some overloading because the int128 data type is not too deeply supported (in Mingw, at least). This is an example of how you can use it to show 2^x-1 up until 2^128-1

__int128_t 和 __uint128_t(无符号 __int128_t)数据类型是 128 位长,是 long long 的两倍(对于 C++ 新手来说是 64 位长)。但是,如果您打算使用它们,那么您需要进行一些重载,因为 int128 数据类型并没有得到太多支持(至少在 Mingw 中)。这是一个如何使用它来显示 2^x-1 直到 2^128-1 的示例

#include <iostream>

char base10_lookup_table[10]={'0','1','2','3','4','5','6','7','8','9'};

std::ostream&
operator<<( std::ostream& dest, __int128 value )
{
    std::ostream::sentry s( dest );
    if ( s ) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[ 128 ];
        char* d = std::end( buffer );
        do
        {
            -- d;
            *d = base10_lookup_table[ tmp % 10 ];
            tmp /= 10;
        } while ( tmp != 0 );
        if ( value < 0 ) {
            -- d;
            *d = '-';
        }
        int len = std::end( buffer ) - d;
        if ( dest.rdbuf()->sputn( d, len ) != len ) {
            dest.setstate( std::ios_base::badbit );
        }
    }
    return dest;
}

std::ostream&
operator<<( std::ostream& dest, unsigned __int128 value )
{
    std::ostream::sentry s( dest );
    if ( s ) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[ 128 ];
        char* d = std::end( buffer );
        do
        {
            -- d;
            *d = base10_lookup_table[ tmp % 10 ];
            tmp /= 10;
        } while ( tmp != 0 );
        if ( value < 0 ) {
            -- d;
            *d = '-';
        }
        int len = std::end( buffer ) - d;
        if ( dest.rdbuf()->sputn( d, len ) != len ) {
            dest.setstate( std::ios_base::badbit );
        }
    }
    return dest;
}



int main ( void )
{
    __uint128_t big_value = 0;      //unsigned int128

    for ( unsigned char i=0; i!=129; ++i )   //I am using an unsigned char because it can hold all the values that will be used
    {
        std::cout << "1 less than 2 to the power of " << int(i) << " = ##代码##" << big_value << "\n";
        big_value |= (__uint128_t)1 << i;
    }

    return 0;    //formal way of exiting
}

The problem with the int128 datatype is that not all compilers may support it.

int128 数据类型的问题在于并非所有编译器都支持它。