Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

java - 整数最大值(max value of integer)

In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767.

(在C中,整数(对于32位计算机)为32位,范围为-32,768至+32,767。)

In Java, the integer is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647.

(在Java中,整数也是32位,但范围为-2,147,483,648至+2,147,483,647。)

I do not understand how the range is different in Java, even though the number of bits is the same.

(我不了解Java中的范围有何不同,即使位数相同。)

Can someone explain this?

(有人可以解释吗?)

  ask by stackuser translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In C , the language itself does not determine the representation of certain datatypes.

(C语言中语言本身无法确定某些数据类型的表示形式。)

It can vary from machine to machine, on embedded systems the int can be 16 bit wide, though usually it is 32 bit.

(它因机器而异,在嵌入式系统上, int可以为16位宽,尽管通常为32位。)

The only requirement is that short int <= int <= long int by size.

(唯一的要求是short int <= int <= long int的大小。)

Also, there is a recommendation that int should represent the native capacity of the processor .

(另外,建议int应该代表处理器的本机容量 。)

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).

(unsigned修饰符允许您将最高位用作值的一部分(否则保留用于符号位)。)

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

In Java , the Java Language Specification determines the representation of the data types.

(Java中 Java语言规范确定数据类型的表示形式。)

The order is: byte 8 bits, short 16 bits, int 32 bits, long 64 bits.

(顺序为: byte 8位, short 16位, int 32位, long 64位。)

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).

(但是,位操作将数字视为无符号的(即,正确处理所有位)。)

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)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...