C++ 中整数类型可以存储什么范围的值?
在 32 位计算机上,unsigned long int
可以保存十位数字 (1,000,000,000 - 9,999,999,999) 吗?
另外,unsigned long int
、long int
、unsigned int
、short int
、的范围是多少>短无符号整数
和int
?
Can unsigned long int
hold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer?
Additionally, what are the ranges of unsigned long int
, long int
, unsigned int
, short int
, short unsigned int
, and int
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以信赖的最小范围是:
short int
和int
:-32,767 到 32,767unsigned Short int
和unsigned int
:0 到 65,535long int
:-2,147,483,647 到 2,147,483,647unsigned long int
:0 到 4,294,967,295这意味着不,
long int
不能用来存储任何 10 位数字。然而,更大的类型,long long int
,在C99 和 C++11 中的 C++(这种类型通常也被支持为为不包含它的旧标准构建的编译器的扩展)。如果您的编译器支持,此类型的最小范围为:long long int
:-9,223,372,036,854,775,807 到 9,223,372,036,854,775,807unsigned long long int
:0 到 18,446,744,073,709,551,615该类型将是足够大(同样,如果你有可用的)。
对于那些认为我在这些下限上犯了错误的人,请注意:编写范围的 C 要求是为了允许 个数的补码或符号数值整数表示,其中最低可表示值和最高可表示值仅符号不同。还允许使用 二进制补码 表示,其中符号位为 1 的值和所有值位 0 是一个陷阱表示,而不是合法值。换句话说,
int
并不需要能够表示值 -32,768。The minimum ranges you can rely on are:
short int
andint
: -32,767 to 32,767unsigned short int
andunsigned int
: 0 to 65,535long int
: -2,147,483,647 to 2,147,483,647unsigned long int
: 0 to 4,294,967,295This means that no,
long int
cannot be relied upon to store any 10-digit number. However, a larger type,long long int
, was 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 long int
: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807unsigned long long int
: 0 to 18,446,744,073,709,551,615So that type will be big enough (again, if you have it available).
A note for those who believe I've made a mistake with these lower bounds: 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 representation rather than a legal value. In other words,
int
is not required to be able to represent the value -32,768.C++ 标准中没有定义数值类型的大小,但定义了最小大小。判断它们在您平台上的大小的方法是使用数字限制
例如,int 的最大值可以通过以下方式找到:
计算机不以 10 为基数工作,这意味着最大值将采用 2n-1 的形式,因为数字的方式的代表在内存中。以八位(1 个字节)为例
最右边的位(数字)设置为 1 时表示 20,下一位为 21,然后为 22 依此类推,直到到达最左边的位,如果数字无符号则表示 27。
所以这个数字表示 26 + 23 = 64 + 8 = 72,因为右起第 4 位和左右第 7 位被设置。
如果我们将所有值设置为 1:
现在的数字是(假设无符号)
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 = 28 - 1
正如我们所看到的,这是可以用 8 位表示的最大可能值。
在我的机器上,int 和 long 是相同的,每个都能够保存 -231 到 231 之间 - 1. 根据我的经验,现代 32 上最常见的大小位台式机。
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
For example, the maximum value for a int can be found by:
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)
The right most bit (number) when set to 1 represents 20, the next bit 21, then 22 and so on until we get to the left most bit which if the number is unsigned represents 27.
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.
If we set all values to 1:
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.
On my machine and int and a long are the same, each able to hold between -231 to 231 - 1. In my experience the most common size on modern 32 bit desktop machine.
要了解您的系统的限制:
请注意,
long long
仅在 C99 和 C++11 中合法。To find out the limits on your system:
Note that
long long
is only legal in C99 and in C++11.这里的其他人将发布 data_sizes 和 precision 等的链接。
我将告诉你如何自己解决这个问题。
编写一个小应用程序来执行以下操作。
这将(取决于编译器和体系结构)打印 2、4 或 8,表示 2 个字节长、4 个字节长等。
假设它是 4。
您现在想要 4 个字节可以存储的最大值,一个字节的最大值是(十六进制)0xFF。四个字节的最大值是 0x 后跟 8 个 f(每个字节一对 f,0x 告诉编译器以下字符串是十六进制数字)。现在更改程序以分配该值并打印结果:
这是 unsigned int 可以保存的最大值,以 10 为基数表示。
现在,对多头、空头和您感兴趣的任何其他 INTEGER 值执行此操作。
注意:此方法不适用于浮点数(即双精度或浮点数)。
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 application that will do the following.
This will (depending on compiler and architecture) print 2, 4 or 8, saying 2 bytes long, 4 bytes long, etc.
Let’s assume it's 4.
You now want the maximum value 4 bytes can store, the maximum value for one byte is (in hexadecimal) 0xFF. The maximum value of four bytes is 0x followed by 8 f's (one pair of f's for each byte, and 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:
that’s the maximum value an unsigned int can hold, shown in base 10 representation.
Now do that for long's, shorts and any other INTEGER value you're curious about.
NB: This approach will not work for floating point numbers (i.e. double or float).
在 C++ 中,现在 int 和其他数据使用二进制补码< /a> 方法。
这意味着范围是:
或-2^31 到 2^31-1。
1 位保留为 0,因此正值是比 2^(31) 减 1。
In C++, now int and other data is stored using the two's complement method.
That means the range is:
or -2^31 to 2^31-1.
1 bit is reserved for 0 so positive value is one less than 2^(31).
您可以使用
limits
头文件中的numeric_limits::min()
和numeric_limits::max()
函数并找出每种数据类型的限制。输出将是:
数据类型限制:
You can use the
numeric_limits<data_type>::min()
andnumeric_limits<data_type>::max()
functions present inlimits
header file and find the limits of each data type.The output will be:
Limits of Data types:
对于无符号数据类型,没有任何符号位,所有位都用于数据;而对于有符号数据类型,MSB 表示符号位和其余位用于数据。
要查找范围,请执行以下操作:
步骤 1:找出给定数据类型的字节数。
步骤 2:应用以下计算。
例如,
对于 unsigned int 大小 = 4 字节(32 位)→ 范围 [0, (2^(32)) - 1]
对于有符号 int 大小 = 4 字节(32 位)→ 范围 [-(2^(32-) 1)), (2^(32-1)) - 1]
For an unsigned data type, there isn't any sign bit and all bits are for data ; whereas for a signed data type, MSB is indicating a sign bit and the remaining bits are for data.
To find the range, do the following things:
Step 1: Find out number of bytes for the given data type.
Step 2: Apply the following calculations.
For example,
For unsigned int size = 4 bytes (32 bits) → Range [0, (2^(32)) - 1]
For signed int size = 4 bytes (32 bits) → Range [-(2^(32-1)), (2^(32-1)) - 1]
不可以,unsigned long int 中只能存储十位数字的一部分,其有效范围是 0 到 4,294,967,295。
你可以参考这个:
http://msdn.microsoft.com/en-us /library/s3f49ktz(VS.80).aspx
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
不
No
您应该查看 numeric_limits<> 的专业化给定类型的模板。它在<限制>内标头。
You should look at the specialisations of the numeric_limits<> template for a given type. It’s in the <limits> header.
当开始学习不同的数据类型时,我发现它很令人困惑,因为理论方法让一切看起来都很复杂。然而,在我自己尝试了一个实际的例子之后,事情变得更加清晰了。
例如,在DECIMAL系统中,8位的最大数字是99999999 。
同样,在BINARY系统中,8位的最大数字将为11111111 。
但是,“int”数据类型的大小可能因系统架构而异,例如 2 字节(16 位)或 4 字节(32 位)。为了确定确切的大小,我们使用“sizeof()”函数。
例如,在32位架构上,“sizeof(int)”是4字节,相当于32位。要表示此架构上“int 4 字节”的最大值,您需要32 个,意思是“unsigned long int”数据类型的(2 x 2^31) - 1 = 4294967295。 (您需要用任何语言编写自己的二进制到十进制转换程序,而不使用预定义的库或方法才能更多地理解这一点,相信我。)
When starting to learn about different data types, I found it confusing due to the theoretical approach which made everything seem complicated. However, after trying out a practical example on my own, things became much clearer.
For instance, in the DECIMAL system, the largest number with 8 digits is 99999999.
Similarly, in the BINARY system, the largest number with 8 bits would be 11111111.
However, the size of the "int" data type can vary depending on the system's architecture, like 2 bytes (16-bit) or 4 bytes (32-bit). To determine the exact size, we use the "sizeof()" function.
For example, on a 32-bit architecture, "sizeof(int)" is 4 bytes, which is equivalent to 32 bits. To represent the largest value of an "int 4 bytes" on this architecture, you would need 32 ones, meaning (2 x 2^31) - 1 = 4294967295 for the "unsigned long int" data type. (You would need to write your own binary to decimal conversion program in any language without using pre-defined library or method to understand this more, trust me.)