整数类型可以在 C++ 中存储什么范围的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1819189/
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 21:15:11  来源:igfitidea点击:

What range of values can integer types store in C++

c++int

提问by yihangho

Can unsigned long inthold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer?

可以unsigned long int在 32 位计算机上保存十位数字(1,000,000,000 - 9,999,999,999)吗?

Additionally, what are the ranges of unsigned long int, long int, unsigned int, short int, short unsigned int, and int?

此外,有什么的范围unsigned long intlong intunsigned intshort intshort unsigned int,和int

回答by caf

The minimumranges you can rely on are:

您可以依赖的最小范围是:

  • short intand int: -32,767 to 32,767
  • unsigned short intand unsigned int: 0 to 65,535
  • long int: -2,147,483,647 to 2,147,483,647
  • unsigned long int: 0 to 4,294,967,295
  • short intint:-32,767 到 32,767
  • unsigned short intunsigned int:0 到 65,535
  • long int: -2,147,483,647 至 2,147,483,647
  • unsigned long int: 0 到 4,294,967,295

This means that no, long intcannotbe relied upon to store any 10 digit number. However, a larger type long long intwas introduced to C in C99 and C++ in C++11 (this type is also often supported as an extension by compilers built for older standards that did not include it). The minimum range for this type, if your compiler supports it, is:

这意味着long int不能依赖于存储任何 10 位数字。但是,long long intC99 中的 C 和 C++11 中的 C++ 中引入了更大的类型(这种类型通常也被为不包含它的旧标准构建的编译器支持作为扩展)。如果您的编译器支持,此类型的最小范围是:

  • long long int: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
  • unsigned long long int: 0 to 18,446,744,073,709,551,615
  • long long int: -9,223,372,036,854,775,807 至 9,223,372,036,854,775,807
  • unsigned long long int: 0 到 18,446,744,073,709,551,615

So that type will be big enough (again, ifyou have it available).

所以那个类型足够大(同样,如果你有的话)。



A note for those who believe I've made a mistake with these lower bounds - I haven't. The C requirements for the ranges are written to allow for ones' complement or sign-magnitude integer representations, where the lowest representable value and the highest representable value differ only in sign. It is also allowed to have a two's complement representation where the value with sign bit 1 and all value bits 0 is a trap representationrather than a legal value. In other words, intis notrequired to be able to represent the value -32,768.

给那些认为我在这些下限上犯了错误的人的说明 - 我没有。编写范围的 C 要求以允许使用 1 的补码或符号大小整数表示,其中可表示的最低值和可表示的最高值仅在符号上不同。还允许具有二进制补码表示,其中符号位为 1 且所有值位为 0 的值是陷阱表示而不是合法值。换言之,int要求是能够代表值-32,768。

回答by Yacoby

The size of the numerical types is not defined in the C++ standard, although the minimum sizes are. The way to tell what size they are on your platform is to use numeric limits

C++ 标准中没有定义数字类型的大小,尽管最小大小是。判断它们在您的平台上的大小的方法是使用数字限制

For example, the maximum value for a int can be found by:

例如,可以通过以下方式找到 int 的最大值:

std::numeric_limits<int>::max();

Computers don't work in base 10, which means that the maximum value will be in the form of 2n-1 because of how the numbers of represent in memory. Take for example eight bits (1 byte)

计算机不能以 10 为基数工作,这意味着最大值将采用 2 n-1的形式,因为数字在内存中的表示方式。以八位(1字节)为例

  0100 1000

The right most bit (number) when set to 1 represents 20, the next bit 21, then 22and so on until we get to the left most bit which if the number is unsigned represents 27.

当设置为 1 时,最右边的位(数字)表示 2 0,下一个位 2 1,然后是 2 2等等,直到我们到达最左边的位,如果数字是无符号的,则表示 2 7

So the number represents 26+ 23= 64 + 8 = 72, because the 4th bit from the right and the 7th bit right the left are set.

所以这个数字代表2 6+ 2 3= 64 + 8 = 72,因为右边的第4位和左边的第7位被设置了。

If we set all values to 1:

如果我们将所有值设置为 1:

11111111

The number is now (assuming unsigned)
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 = 28- 1
And as we can see, that is the largest possible value that can be represented with 8 bits.

这个数字现在是(假设unsigned
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 = 2 8- 1
正如我们所看到的,这是可以用 8 位表示的最大可能值。

On my machine and int and a long are the same, each able to hold between -231to 231- 1. In my experience the most common size on modern 32 bit desktop machine.

在我的机器上,int 和 long 是相同的,每个都可以容纳 -2 31到 2 31- 1。根据我的经验,这是现代 32 位台式机上最常见的大小。

回答by Hal Canary

To find out the limits on yoursystem:

要了解您的系统的限制:

#include <iostream>
#include <limits>
int main(int, char **) {
  std::cout
    << static_cast< int >(std::numeric_limits< char >::max()) << "\n"
    << static_cast< int >(std::numeric_limits< unsigned char >::max()) << "\n"
    << std::numeric_limits< short >::max() << "\n"
    << std::numeric_limits< unsigned short >::max() << "\n"
    << std::numeric_limits< int >::max() << "\n"
    << std::numeric_limits< unsigned int >::max() << "\n"
    << std::numeric_limits< long >::max() << "\n"
    << std::numeric_limits< unsigned long >::max() << "\n"
    << std::numeric_limits< long long >::max() << "\n"
    << std::numeric_limits< unsigned long long >::max() << "\n";
}

Note that long longis only legal in C99 and in C++11.

请注意,这long long仅在 C99 和 C++11 中合法。

回答by Binary Worrier

Other folks here will post links to data_sizes and precisions etc.
I'm going to tell you how to figure it out yourself.
Write a small app that will do the following.

这里的其他人会发布指向 data_sizes 和 precisions 等的链接。
我将告诉你如何自己弄清楚。
编写一个小应用程序,将执行以下操作。

unsigned int ui;
std::cout <<  sizeof(ui));

this will (depending on compiler and archicture) print 2, 4 or 8, saying 2 bytes long, 4 bytes long etc.

这将(取决于编译器和架构)打印 2、4 或 8,即 2 个字节长、4 个字节长等。

Lets assume it's 4.

让我们假设它是 4。

You now want the maximum value 4 bytes can store, the max value for one byte is (in hex)0xFF. The max value of four bytes is 0x followed by 8 f's (one pair of f's for each byte, the 0x tells the compiler that the following string is a hex number). Now change your program to assign that value and print the result

您现在想要 4 个字节可以存储的最大值,一个字节的最大值是(十六进制)0xFF。四个字节的最大值是 0x 后跟 8 个 f(每个字节一对 f,0x 告诉编译器以下字符串是十六进制数)。现在更改您的程序以分配该值并打印结果

unsigned int ui = 0xFFFFFFFF;
std::cout <<  ui;

Thats the max value an unsigned int can hold, shown in base 10 representation.

这是 unsigned int 可以容纳的最大值,以基数 10 表示。

Now do that for long's, shorts and any other INTEGER value you're curious about.

现在对多头、空头和您感兴趣的任何其他 INTEGER 值执行此操作。

NB: This approach will not work for floating point numbers (i.e. double or float).

注意:此方法不适用于浮点数(即双精度或浮点数)。

Hope this helps

希望这可以帮助

回答by Shah Rukh Qasim

In C++, now int and other data is stored using 2's compliment method. That means the range is:

在 C++ 中,现在 int 和其他数据使用 2 的补码方法存储。这意味着范围是:

-2147483648 to 2147483647

or -2^31 to 2^31-1

或 -2^31 到 2^31-1

1 bit is reserved for 0 so positive value is one less than 2^(31)

1 位为 0 保留,因此正值比 2^(31) 小 1

回答by insearchofcode

You can use the numeric_limits<data_type>::min()and numeric_limits<data_type>::max()functions present in limitsheader file and find the limits of each data type.

您可以使用头文件中的numeric_limits<data_type>::min()numeric_limits<data_type>::max()函数limits并找到每种数据类型的限制。

#include <iostream>
#include <limits>
using namespace std;
int main()
{
    cout<<"Limits of Data types:\n";    
    cout<<"char\t\t\t: "<<static_cast<int>(numeric_limits<char>::min())<<" to "<<static_cast<int>(numeric_limits<char>::max())<<endl;
    cout<<"unsigned char\t\t: "<<static_cast<int>(numeric_limits<unsigned char>::min())<<" to "<<static_cast<int>(numeric_limits<unsigned char>::max())<<endl;
    cout<<"short\t\t\t: "<<numeric_limits<short>::min()<<" to "<<numeric_limits<short>::max()<<endl;
    cout<<"unsigned short\t\t: "<<numeric_limits<unsigned short>::min()<<" to "<<numeric_limits<unsigned short>::max()<<endl;
    cout<<"int\t\t\t: "<<numeric_limits<int>::min()<<" to "<<numeric_limits<int>::max()<<endl;
    cout<<"unsigned int\t\t: "<<numeric_limits<unsigned int>::min()<<" to "<<numeric_limits<unsigned int>::max()<<endl;
    cout<<"long\t\t\t: "<<numeric_limits<long>::min()<<" to "<<numeric_limits<long>::max()<<endl;
    cout<<"unsigned long\t\t: "<<numeric_limits<unsigned long>::min()<<" to "<<numeric_limits<unsigned long>::max()<<endl;
    cout<<"long long\t\t: "<<numeric_limits<long long>::min()<<" to "<<numeric_limits<long long>::max()<<endl;
    cout<<"unsiged long long\t: "<<numeric_limits<unsigned long long>::min()<<" to "<<numeric_limits<unsigned long long>::max()<<endl;
    cout<<"float\t\t\t: "<<numeric_limits<float>::min()<<" to "<<numeric_limits<float>::max()<<endl;
    cout<<"double\t\t\t: "<<numeric_limits<double>::min()<<" to "<<numeric_limits<double>::max()<<endl;
    cout<<"long double\t\t: "<<numeric_limits<long double>::min()<<" to "<<numeric_limits<long double>::max()<<endl;
}

The output will be: Limits of Data types:

输出将是: 数据类型的限制:

  • char : -128 to 127
  • unsigned char : 0 to 255
  • short : -32768 to 32767
  • unsigned short : 0 to 65535
  • int : -2147483648 to 2147483647
  • unsigned int : 0 to 4294967295
  • long : -2147483648 to 2147483647
  • unsigned long : 0 to 4294967295
  • long long : -9223372036854775808 to 9223372036854775807
  • unsigned long long : 0 to 18446744073709551615
  • float : 1.17549e-038 to 3.40282e+038
  • double : 2.22507e-308 to 1.79769e+308
  • long double : 3.3621e-4932 to 1.18973e+4932
  • 字符:-128 到 127
  • 无符号字符:0 到 255
  • 短:-32768 到 32767
  • 无符号短:0 到 65535
  • 整数:-2147483648 到 2147483647
  • 无符号整数:0 到 4294967295
  • 长:-2147483648 到 2147483647
  • 无符号长:0 到 4294967295
  • 长长:-9223372036854775808 到 9223372036854775807
  • 无符号长长:0 到 18446744073709551615
  • 浮动:1.17549e-038 到 3.40282e+038
  • 双:2.22507e-308 到 1.79769e+308
  • 长双:3.3621e-4932 到 1.18973e+4932

回答by Ashish

For unsigned data typethere is no sign bit and all bits are for data ; whereas for signed data typeMSB is indicated sign bit and remaining bits are for data.

对于无符号数据类型,没有符号位,所有位都用于数据;而对于有符号数据类型,MSB 表示符号位,其余位用于数据。

To find the range do following things :

要找到范围,请执行以下操作:

Step:1 -> Find out no of bytes for the give data type.

步骤:1 -> 找出给定数据类型的字节数。

Step:2 -> Apply following calculations.

步骤:2 -> 应用以下计算。

      Let n = no of bits in data type  

      For signed data type ::
            Lower Range = -(2^(n-1)) 
            Upper Range = (2^(n-1)) - 1)  

      For unsigned data type ::
            Lower Range = 0 
            Upper Range = (2^(n)) - 1 

For e.g.

例如

For unsigned int size = 4 bytes (32 bits) --> Range [0 , (2^(32)) - 1]

对于无符号整数大小 = 4 字节(32 位)--> 范围 [0 , (2^(32)) - 1]

For signed int size = 4 bytes (32 bits) --> Range [-(2^(32-1)) , (2^(32-1)) - 1]

对于有符号整数大小 = 4 字节(32 位)--> 范围 [-(2^(32-1)) , (2^(32-1)) - 1]

回答by justin

Can unsigned long int hold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer.

unsigned long int 能否在 32 位计算机上保存十位数字 (1,000,000,000 - 9,999,999,999)。

No

回答by Raymond

No, only part of ten digits number can be stored in a unsigned long int whose valid range is 0 to 4,294,967,295 . you can refer to this: http://msdn.microsoft.com/en-us/library/s3f49ktz(VS.80).aspx

不,只有十位数字的一部分可以存储在 unsigned long int 中,其有效范围是 0 到 4,294,967,295 。你可以参考这个:http: //msdn.microsoft.com/en-us/library/s3f49ktz(VS.80).aspx

回答by PaulJWilliams

You should look at the specialisations of the numeric_limits<> template for a given type. Its in the header.

您应该查看给定类型的 numeric_limits<> 模板的特化。它在标题中。