整数的最大值 [英] max value of integer
问题描述
在 C 中,整数(对于 32 位机器)是 32 位,范围从 -32,768 到 +32,767.在 Java 中,整数(long)也是 32 位,但范围从 -2,147,483,648 到 +2,147,483,647.
In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767. In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647.
我不明白 Java 中的范围有何不同,即使位数相同.有人能解释一下吗?
I do not understand how the range is different in Java, even though the number of bits is the same. Can someone explain this?
推荐答案
在 C 中, 语言本身并不决定某些数据类型的表示.它可能因机器而异,在嵌入式系统上,int
可以是 16 位宽,但通常是 32 位.
In C, the language itself does not determine the representation of certain datatypes. It can vary from machine to machine, on embedded systems the int
can be 16 bit wide, though usually it is 32 bit.
唯一的要求是 short int
<= int
<= long int
按大小.此外,还建议 int
应代表处理器的本机容量.
The only requirement is that short int
<= int
<= long int
by size. Also, there is a recommendation that int
should represent the native capacity of the processor.
所有类型都有签名.unsigned
修饰符允许您使用最高位作为值的一部分(否则它是为符号位保留的).
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).
以下是可能的数据类型的可能值的简短表格:
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
在 Java 中,Java 语言规范决定了数据类型的表示.
In Java, the Java Language Specification determines the representation of the data types.
顺序为:byte
8 bits, short
16 bits, int
32 bits, long
64 bits.所有这些类型都是签名,没有未签名的版本.但是,位操作将数字视为无符号数(即正确处理所有位).
The order is: byte
8 bits, short
16 bits, int
32 bits, long
64 bits. 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).
字符数据类型 char
为 16 位宽,无符号,并使用 UTF-16 编码保存字符(但是,可以分配一个 char
一个任意的无符号 16 位整数,表示一个无效的字符代码点)
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)
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
这篇关于整数的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!