为什么Java中的字节范围是-128到127?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3621067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:47:40  来源:igfitidea点击:

Why is the range of bytes -128 to 127 in Java?

javabyte

提问by Bad Request

I don't understand why the lowest value a byte can take is -128. I can see that the highest value is 127, because it's 01111111in binary, but how does one represent -128with only 8 bits, one of which is used for the sign? Positive 128 would already be 8-bit, i.e. 10000000, and then you would need a 9th bit to represent the negative sign.

我不明白为什么一个字节可以取的最小值是-128. 我可以看到最高值是127,因为它是01111111二进制的,但是如何-128仅用 8 位表示,其中 1 位用于符号?正 128 已经是 8 位,即10000000,然后您需要第 9 位来表示负号。

Could someone please help explain this to me.

有人可以帮我解释一下。

采纳答案by Tyler McHenry

The answer is two's complement.

答案是二进制补码

In short, Java (and most modern languages) do not represent signed integers using signed-magnitude representation. In other words, an 8-bit integer is not a sign bit followed by a 7-bit unsigned integer.

简而言之,Java(和大多数现代语言)不使用有符号大小表示来表示有符号整数。换句话说,一个 8 位整数不是一个符号位后跟一个 7 位无符号整数。

Instead, negative integers are represented in a system called two's complement, which allows easier arithmetic processing in hardware, and also eliminates the potential ambiguity of having positive zero and negative zero. A side effect of eliminating negative zero is that there is always one extra negative number available at the bottom of the range.

相反,负整数在称为二进制补码的系统中表示,这使得硬件中的算术处理更容易,并且还消除了具有正零和负零的潜在歧义。消除负零的一个副作用是在范围的底部总是有一个额外的负数可用。

Another interesting property of two's complement systems is that the first bit does effectivelyfunction as a sign indicator (i.e. all numbers beginning with the bit 1 are negative), but the next seven bits are not to be interpreted on their own as an unsigned number to which the sign bit is applied.

二进制补码系统的另一个有趣特性是第一位确实有效地用作符号指示符(即所有以位 1 开头的数字都是负数),但接下来的七位不能被单独解释为无符号数应用了符号位。

Two's complement isn't terribly complicated, but getting an initial good grip on what two's complement is and how and why it works is probably beyond the scope of an SO answer. Start with the Wikipedia article, or google the term for more resources.

二进制补码并不是非常复杂,但是初步了解二进制补码是什么以及它如何以及为什么起作用可能超出了 SO 答案的范围。从维基百科文章开始,或谷歌搜索更多资源。

To try to briefly address your query about -128, the fundamental idea behind generating a two's complement number is to take the unsigned form of the number, invert all of the bits and add one. So unsigned 128 is 10000000. Inverted, it's 01111111, and adding one gets 10000000 again. So in a two's complement system, 10000000 is unambiguously -128 and not +128. Numbers greater than or equal to +128 simply cannot be represented in 8 bits using a two's complement system because they would be ambiguous with the forms of negative numbers.

为了尝试简要解决您关于 -128 的查询,生成二进制补码数背后的基本思想是采用数字的无符号形式,反转所有位并加一。所以无符号的 128 是 10000000。倒过来就是 01111111,再加一个又是 10000000。所以在二进制补码系统中,10000000 是明确的 -128 而不是 +128。大于或等于 +128 的数字不能使用二进制补码系统用 8 位表示,因为它们与负数的形式会产生歧义。

回答by Tamás Szelei

As James pointed out in his comment, it's because that's how two's complement works.

正如詹姆斯在他的评论中指出的那样,这是因为这就是二进制补码的工作方式。

If we put it in other terms, you can represent 2^8 = 256 kind of values. which is, in this case used as 128 negative numbers, 127 positive numbers, and zero. If we used 7 bits to represent the value, +1 bit for a sign, we could represent one less value and would also have two zeros (which would be very unfortunate as comparing two values would be more complicated because of that).

如果换个说法,可以表示 2^8 = 256 种值。即,在本例中用作 128 个负数、127 个正数和零。如果我们使用 7 位来表示值,+1 位表示符号,我们可以表示少一个值并且还会有两个零(这将非常不幸,因为比较两个值会因此变得更加复杂)。

回答by Andrei Bazavan

in java all the variables like byte short int long float double are written as signed . so is very simple the head bit always specifies what is( negative or positive), but because the numbers are dividable by 2 half is shifted as negative , 0 is positive by default . so it looks like this :

在 java 中,所有变量,如 byte short int long float double 都写为 signed 。所以很简单,头位总是指定什么是(负数或正数),但因为数字可以被 2 整除,所以被移为负数,默认情况下 0 是正数。所以它看起来像这样:

this is positive
+|0001001
1|0001001
this is negative
-|0001001
0|0001001
as a byte short a negative is
-000000011111111
0000000011111111

这是正数
+|0001001
1|0001001
这是负数
-|0001001
0|0001001
作为一个字节短,负数是
-000000011111111
0000000011111111

回答by Sic

Basic numeric types can represent 2^n numbers. Look at a case n=2. You can represent four cases, lets call them a, b, c, d. Then you can agree to either a=-2, b=-1, c=0, d=1(this is accepted way) or a=-1, b=0, c=1, d=2(Possible, but not used). So, if you only have one zero and hold 2^n states your abs(min) != maxIncreasing the nmoves the borders, but abs(min) != maxstill holds.

基本数字类型可以表示 2^n 个数字。看一个情况 n=2。您可以表示四种情况,我们称它们为 a、b、c、d。然后您可以同意a=-2, b=-1, c=0, d=1(这是接受的方式)或 a=-1, b=0, c=1, d=2(可能,但未使用)。因此,如果您只有一个零并持有 2^n 表示您abs(min) != max增加n移动边界,但abs(min) != max仍然成立。

回答by Mina Fawzy

byte consist of 8 bit ---> 1 bit sign (positive or negative ) 7 bit value

字节由 8 位组成 ---> 1 位符号(正或负)7 位值

so the range -2^7 negative (-128 ) to 2^7 -1 positive(127)

所以范围 -2^7 负(-128)到 2^7 -1 正(127)

回答by David Hollowell - MSFT

Without getting into two's complement: 2^8 (since a byte is 8 digits and can have 1 of 2 values) = 256, so the most individual values a byte can represent is 256. so, representing the numbers -128 to -1 is half our range. I believe the question here is why is the max positive value 127 rather than 128. This is because we have to represent the number 0, so inclusively 0-127 is the other 128 possibilities of our range.

没有进入二进制补码:2^8(因为一个字节是 8 位数字并且可以有 2 个值中的 1 个)= 256,所以一个字节可以表示的最多单个值是 256。因此,表示数字 -128 到 -1 是我们范围的一半。我相信这里的问题是为什么最大正值是 127 而不是 128。这是因为我们必须表示数字 0,所以 0-127 是我们范围的其他 128 个可能性。

If we were only allowing positive values, such as an unsigned byte where negative numbers aren't possible, the range would be 0-255, since these are 256 different values (including the 0).

如果我们只允许正值,例如不可能出现负数的无符号字节,则范围将是 0-255,因为这些是 256 个不同的值(包括 0)。

回答by Ad Infinitum

Two′s complement works as follows;

二的补码工作如下;

A byte consists of 8 bits.

一个字节由 8 位组成。

00000000 means 0

11111111 means 255

00000000 表示 0

11111111 表示 255

However, if the numbers were presented like that, we would not differentiate between whether the resulting number is positive or negative. Because of this reason, the bit on the left side gives us this information. If the bit on the left side is 0, you can start adding the value of other bits on the top of zero. If the bit is 1, you should start adding on the top of -128. Because the bit on the left side is two to the power of seven.

但是,如果数字是这样呈现的,我们将无法区分结果数字是正数还是负数。由于这个原因,左侧的位为我们提供了此信息。如果左侧的位是0,则可以开始在顶部添加其他位的值zero。如果位是1,您应该开始在-128. 因为左边的位是二的七次方。

Examples;

例子;

In these examples, the bit on the left side is 1, it means we are adding the values of other bits on the top of -128.

在这些示例中,左侧的位为 1,这意味着我们将在 -128 的顶部添加其他位的值。

10000000 = -128 (-128 + 0)

10000001 = -127 (-128 + 1)

10000011 = -125 (-128 + 3)

10000111 = -121 (-128 + 7)

10000000 = -128 (-128 + 0)

10000001 = -127 (-128 + 1)

10000011 = -125 (-128 + 3)

10000111 = -121 (-128 + 7)

Same bits but this time, the bit on the left is 0. That means we are starting to add on the top of 0.

相同的位,但这次,左边的位是0。这意味着我们开始在0.

00000000 = 0 (0 + 0)

00000001 = 1 (0 + 1)

00000011 = 3 (0 + 3)

00000111 = 7 (0 + 7)

00000000 = 0 (0 + 0)

00000001 = 1 (0 + 1)

00000011 = 3 (0 + 3)

00000111 = 7 (0 + 7)

If we are ok until now, the answer to your question,

如果我们到现在都还好,你的问题的答案,

the smallest possible number

最小的可能数

10000000 = -128

10000000 = -128

the biggest possible number

最大可能的数字

011111111 = 127

011111111 = 127

That is why the range is between -128 and 127.

这就是范围介于-128 和 127之间的原因。

回答by Rashid Ali

after taking two's compliment of number we always left with one state of representing number extra so we turn that state to -128.

在对数字进行 2 的恭维后,我们总是留下一种表示额外数字的状态,因此我们将该状态变为 -128。