在C语言中,整数(对于32位机器)是32位,范围从-32,768到+32,767。 在Java中,整数(long)也是32位,但范围从- 2147,483,648到+ 2147,483,647。
我不明白Java中的范围是如何不同的,即使位数是相同的。有人能解释一下吗?
在C语言中,整数(对于32位机器)是32位,范围从-32,768到+32,767。 在Java中,整数(long)也是32位,但范围从- 2147,483,648到+ 2147,483,647。
我不明白Java中的范围是如何不同的,即使位数是相同的。有人能解释一下吗?
当前回答
实际上,int的长度(以比特为单位)取决于编译器的实现。
例如,在我的Ubuntu 64位上,我有32位的短,而在另一个32位的Ubuntu版本上,它是16位的。
其他回答
在标准C语言中,你可以使用INT_MAX作为最大的“int”值,这个常量必须在“limits.h”中定义。为其他类型(http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.5.html)定义了类似的常量,如所述,这些常量依赖于实现,但根据标准中指定的每种类型的最小位有一个最小值。
发帖者把他们的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
java int的严格等效是C中的long int。
编辑: 如果定义了int32_t,那么它在精度方面是等效的。长整型保证了Java整型的精度,因为它的大小保证至少为32位。
这是因为在32位机器上的C - integer并不意味着使用32位来存储它,它也可能是16位。它取决于机器(依赖于实现)。
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.
就是这样。这取决于机器,而不是语言。