简体   繁体   English

integer 的最大值

[英]max value of integer

In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767.在 C 中,integer(用于 32 位机器)为 32 位,范围从 -32,768 到 +32,767。 In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647.在 Java 中,整数(long)也是 32 位,但范围从 -2,147,483,648 到 +2,147,483,647。

I do not understand how the range is different in Java, even though the number of bits is the same.我不明白 Java 的范围有何不同,即使位数相同。 Can someone explain this?有人可以解释一下吗?

In C , the language itself does not determine the representation of certain datatypes.C 中语言本身并不决定某些数据类型的表示。 It can vary from machine to machine, on embedded systems the int can be 16 bit wide, though usually it is 32 bit.它可以因机器而异,在嵌入式系统上, int可以是 16 位宽,但通常是 32 位。

The only requirement is that short int <= int <= long int by size.唯一的要求是short int <= int <= long int按大小计算。 Also, there is a recommendation that int should represent the native capacity of the processor .此外,建议int应代表处理器的本机容量

All types are signed.所有类型都有签名。 The unsigned modifier allows you to use the highest bit as part of the value (otherwise it is reserved for the sign bit). unsigned修饰符允许您使用最高位作为值的一部分(否则它是为符号位保留的)。

Here's a short table of the possible values for the possible data types:以下是可能的数据类型的可能值的简短表格:

          width                     minimum                         maximum
signed    8 bit                        -128                            +127
signed   16 bit                     -32 768                         +32 767
signed   32 bit              -2 147 483 648                  +2 147 483 647
signed   64 bit  -9 223 372 036 854 775 808      +9 223 372 036 854 775 807
unsigned  8 bit                           0                            +255
unsigned 16 bit                           0                         +65 535
unsigned 32 bit                           0                  +4 294 967 295
unsigned 64 bit                           0     +18 446 744 073 709 551 615

In Java , the Java Language Specification determines the representation of the data types.Java 中 Java 语言规范确定了数据类型的表示。

The order is: byte 8 bits, short 16 bits, int 32 bits, long 64 bits.顺序是: byte 8位, short 16位, int 32位, long 64位。 All of these types are signed , there are no unsigned versions.所有这些类型都是签名的,没有未签名的版本。 However, bit manipulations treat the numbers as they were unsigned (that is, handling all bits correctly).但是,位操作将数字视为无符号数(即正确处理所有位)。

The character data type char is 16 bits wide, unsigned , and holds characters using UTF-16 encoding (however, it is possible to assign a char an arbitrary unsigned 16 bit integer that represents an invalid character codepoint)字符数据类型char是 16 位宽, unsigned ,并使用 UTF-16 编码保存字符(但是,可以为char分配一个代表无效字符代码点的任意无符号 16 位整数)

          width                     minimum                         maximum

SIGNED
byte:     8 bit                        -128                            +127
short:   16 bit                     -32 768                         +32 767
int:     32 bit              -2 147 483 648                  +2 147 483 647
long:    64 bit  -9 223 372 036 854 775 808      +9 223 372 036 854 775 807

UNSIGNED
char     16 bit                           0                         +65 535

In C, the integer(for 32 bit machine) is 32 bit and it ranges from -32768 to +32767.在 C 中,整数(对于 32 位机器)是 32 位,范围从 -32768 到 +32767。

Wrong.错误的。 32-bit signed integer in 2's complement representation has the range -2 31 to 2 31 -1 which is equal to -2,147,483,648 to 2,147,483,647. 2 的补码表示中的 32 位有符号整数的范围为 -2 31到 2 31 -1,等于 -2,147,483,648 到 2,147,483,647。

A 32 bit integer ranges from -2,147,483,648 to 2,147,483,647. 32 位整数的范围从 -2,147,483,648 到 2,147,483,647。 However the fact that you are on a 32-bit machine does not mean your C compiler uses 32-bit integers.然而,您在 32 位机器上的事实并不意味着您的C编译器使用 32 位整数。

The C language definition specifies minimum ranges for various data types. C 语言定义指定了各种数据类型的最小范围。 For int , this minimum range is -32767 to 32767, meaning an int must be at least 16 bits wide.对于int ,此最小范围是 -32767 到 32767,这意味着int必须至少为16 位宽。 An implementation is free to provide a wider int type with a correspondingly wider range.实现可以自由地提供具有相应更宽范围的更宽的int类型。 For example, on the SLES 10 development server I work on, the range is -2147483647 to 2137483647.例如,在我工作的 SLES 10 开发服务器上,范围是 -2147483647 到 2137483647。

There are still some systems out there that use 16-bit int types (All The World Is Not A VAX x86), but there are plenty that use 32-bit int types, and maybe a few that use 64-bit.仍然有一些系统使用 16 位int类型(All The World Is Not A VAX x86),但有很多系统使用 32 位int类型,可能还有一些使用 64 位。

The C language was designed to run on different architectures. C 语言旨在运行在不同的体系结构上。 Java was designed to run in a virtual machine that hides those architectural differences. Java 旨在运行在隐藏这些架构差异的虚拟机中。

The strict equivalent of the java int is long int in C. java int的严格等价物是 C 中的long int

Edit: If int32_t is defined, then it is the equivalent in terms of precision.编辑:如果定义了int32_t ,那么它在精度方面是等效的。 long int guarantee the precision of the java int , because it is guarantee to be at least 32 bits in size. long int保证 java int的精度,因为它保证大小至少为 32 位。

The poster has their java types mixed up.海报混淆了他们的 java 类型。 in java, his C in is a short: short (16 bit) = -32768 to 32767 int (32 bit) = -2,147,483,648 to 2,147,483,647在java中,他的C in是short:short (16 bit) = -32768 to 32767 int (32 bit) = -2,147,483,648 to 2,147,483,647

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

That's because in C - integer on 32 bit machine doesn't mean that 32 bits are used for storing it, it may be 16 bits as well.那是因为在 C - 32 位机器上的整数并不意味着 32 位用于存储它,它也可能是 16 位。 It depends on the machine (implementation-dependent).这取决于机器(取决于实现)。

Actually the size in bits of the int , short , long depends on the compiler implementation.实际上intshortlong位大小取决于编译器的实现。

Eg on my Ubuntu 64 bit I have short in 32 bits, when on another one 32bit Ubuntu version it is 16 bit.例如,在我的 Ubuntu 64 位上,我的32short ,而在另一个 32 位 Ubuntu 版本上,它是16位。

In C range for __int32 is –2147483648 to 2147483647. See here for full ranges.在 C 中,__int32 的范围是 –2147483648 到 2147483647。有关完整范围,请参见此处。

unsigned short 0 to 65535
signed short –32768 to 32767
unsigned long 0 to 4294967295
signed long –2147483648 to 2147483647

There are no guarantees that an 'int' will be 32 bits, if you want to use variables of a specific size, particularly when writing code that involves bit manipulations, you should use the 'Standard Integer Types'.不能保证“int”是 32 位,如果您想使用特定大小的变量,尤其是在编写涉及位操作的代码时,您应该使用“标准整数类型”。

In Java在 Java 中

The int data type is a 32-bit signed two's complement integer. int 数据类型是一个 32 位有符号二进制补码整数。 It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).它的最小值为 -2,147,483,648,最大值为 2,147,483,647(含)。

It is actually really simple to understand, you can even compute it with the google calculator: you have 32 bits for an int and computers are binary, therefore you can have 2 values per bit (spot).它实际上很容易理解,你甚至可以用谷歌计算器来计算它:你有 32 位的 int 而计算机是二进制的,因此你可以有 2 个值/位(点)。 if you compute 2^32 you will get the 4,294,967,296.如果您计算 2^32,您将得到 4,294,967,296。 so if you divide this number by 2, (because half of them are negative integers and the other half are positive), then you get 2,147,483,648.所以如果你把这个数字除以 2,(因为一半是负整数,另一半是正整数),那么你得到 2,147,483,648。 and this number is the biggest int that can be represented by 32 bits, although if you pay attention you will notice that 2,147,483,648 is greater than 2,147,483,647 by 1, this is because one of the numbers represents 0 which is right in the middle unfortunately 2^32 is not an odd number therefore you dont have only one number in the middle, so the possitive integers have one less cipher while the negatives get the complete half 2,147,483,648.这个数字是可以用 32 位表示的最大整数,但如果你注意你会注意到 2,147,483,648 比 2,147,483,647 大 1,这是因为其中一个数字代表 0,不幸的是 2^ 32 不是奇数,因此中间不会只有一个数字,因此正整数的密码少一个,而负整数则是完整的一半 2,147,483,648。

And thats it.就是这样。 It depends on the machine not on the language.这取决于机器而不是语言。

in standard C, you can use INT_MAX as the maximum 'int' value, this constant must be defined in "limits.h".在标准 C 中,您可以使用 INT_MAX 作为最大 'int' 值,该常量必须在“limits.h”中定义。 Similar constants are defined for other types ( http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.5.html ), as stated, these constant are implementation-dependent but have a minimum value according to the minimum bits for each type, as specified in the standard.为其他类型定义了类似的常量( http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.5.html ),如上所述,这些常量与实现相关,但根据最小位具有最小值对于每种类型,如标准中所规定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM