在C语言中,整数(对于32位机器)是32位,范围从-32,768到+32,767。 在Java中,整数(long)也是32位,但范围从- 2147,483,648到+ 2147,483,647。

我不明白Java中的范围是如何不同的,即使位数是相同的。有人能解释一下吗?


java int的严格等效是C中的long int。

编辑: 如果定义了int32_t,那么它在精度方面是等效的。长整型保证了Java整型的精度,因为它的大小保证至少为32位。

这是因为在32位机器上的C - integer并不意味着使用32位来存储它,它也可能是16位。它取决于机器(依赖于实现)。

在C语言中,__int32的范围是-2147483648到2147483647。看这里的全范围。

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

不能保证'int'将是32位,如果你想使用特定大小的变量,特别是在编写涉及位操作的代码时,你应该使用'标准整数类型'。

在Java中

int数据类型是一个32位有符号的二补整数。它的最小值为-2,147,483,648,最大值为2,147,483,647(含)。

32位整数,取值范围为- 2147,483,648 ~ 2147,483,647。然而,你在32位机器上的事实并不意味着你的C编译器使用32位整数。

在C语言中,整数(对于32位机器)是32位,范围从-32768到+32767。

错,32位有符号整数在2的补式表示中的范围是-231到231-1,等于-2,147,483,648到2,147,483,647。

在C语言中,语言本身并不决定某些数据类型的表示。它可能因机器而异,在嵌入式系统上int可以是16位宽,尽管通常是32位。

唯一的要求是short int <= int <= long int by size。此外,还有一种建议是int应该表示处理器的本机容量。

所有类型都有符号。无符号修饰符允许您使用最高位作为值的一部分(否则将为符号位保留)。

下面是可能的数据类型的可能值的简短表格:

          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

在Java中,Java语言规范决定了数据类型的表示。

顺序是:字节8位,短16位,整数32位,长64位。所有这些类型都是有符号的,没有无符号的版本。然而,位操作将数字视为无符号的(即正确处理所有位)。

字符数据类型char宽为16位,无符号,并使用UTF-16编码保存字符(但是,可以为字符分配任意无符号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

实际上,int的长度(以比特为单位)取决于编译器的实现。

例如,在我的Ubuntu 64位上,我有32位的短,而在另一个32位的Ubuntu版本上,它是16位的。

C语言定义为各种数据类型指定了最小范围。对于int型,这个最小范围是-32767到32767,这意味着int型必须至少有16位宽。实现可以自由地提供更广泛的int类型和相应的更广泛的范围。例如,在我工作的SLES 10开发服务器上,范围是-2147483647到2137483647。

仍然有一些系统使用16位整型类型(All The World Is Not A VAX x86),但有很多系统使用32位整型类型,可能也有少数系统使用64位整型类型。

C语言被设计成可以在不同的体系结构上运行。Java被设计为在虚拟机中运行,从而隐藏了这些架构上的差异。

发帖者把他们的java类型搞混了。 在java中,他的C in是一个简短的: 短(16位)= -32768 ~ 32767 Int(32位)= -2,147,483,648到2,147,483,647

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

在标准C语言中,你可以使用INT_MAX作为最大的“int”值,这个常量必须在“limits.h”中定义。为其他类型(http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.5.html)定义了类似的常量,如所述,这些常量依赖于实现,但根据标准中指定的每种类型的最小位有一个最小值。

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). if you compute 2^32 you will get the 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. 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.

就是这样。这取决于机器,而不是语言。