在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中的范围是如何不同的,即使位数是相同的。有人能解释一下吗?
当前回答
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.
就是这样。这取决于机器,而不是语言。
其他回答
实际上,int的长度(以比特为单位)取决于编译器的实现。
例如,在我的Ubuntu 64位上,我有32位的短,而在另一个32位的Ubuntu版本上,它是16位的。
在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
发帖者把他们的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.
就是这样。这取决于机器,而不是语言。