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

我不明白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

其他回答

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.

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

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

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

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

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

发帖者把他们的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

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