为什么Java中整数的-1零填充右移1=2147483647 ?

时间:2022-07-30 16:58:07

For the program below:

下面的程序:

public class ZeroFillRightShift
{
  public static void main(String args[])
  {
    int x = -1;
    int y = x>>>1;
    System.out.println("x = " + x);
    System.out.println("y = " + y);
  }
}

I get the output as follows:

输出如下:

x = -1
y = 2147483647

The result that I got for -1>>>1 is 2147483647. If it’s the sign bit that has to be shifted, as I learned, the result should be 1073741824. Why is it 2147483647 then?

得到-1>>>1的结果是2147483647。如果符号位必须被移位,那么结果应该是1073741824。为什么是2147483647 ?

The following image illustrates my problem more clearly: 为什么Java中整数的-1零填充右移1=2147483647 ?

下面的图片更清楚地说明了我的问题:

4 个解决方案

#1


7  

The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

无符号右移位运算符>>>将0转换为最左位置,而“>>”后的最左位置依赖于符号的扩展。

So, -1 is shifted right one bit with zero-extension, which means it will insert a 0 into the leftmost position. Remember, we're dealing with two's complement here:

所以-1被右移了一个位,0延伸,这意味着它会把0插入到最左边的位置。记住,这里是2的补码

-1 is: 11111111111111111111111111111111 or 0xFFFFFFFF in Hex

-1是:11111111111111111111111111111111或十六进制中的0xffffff

-1 >>> 1 is 01111111111111111111111111111111 or 0x7FFFFFFF in Hex,
which is 231 - 1 == 2147483647

>> 1为0111111111111111111111111111111111111111111111111111111111111111111111111111或Hex中的0x7fffff,即231 -1 = 2147483647

Here's a JLS reference for shift operators.

这里是移位操作符的JLS引用。

You seemed to be confused about two's complement. 31 bits are used for the value and the bit farthest to the left is used for the sign. Since you're only shifting by 1 bit, the signed bit becomes a 0, which means positive, and the result is the greatest positive number than an int can represent.

你似乎对二的补数感到困惑。31位用于值,最左边的位用于符号。因为你只移动了1位,有符号的位变成了0,这意味着正的,结果是一个比int所能表示的最大的正数。

Perhaps another example will help. Let's consider the following:

也许另一个例子会有帮助。让我们考虑以下:

System.out.println(-2 >> 1); //prints -1

-2 = 11111111111111111111111111111110

2 = 11111111111111111111111111111110

If we use a signed right shift, we get: 11111111111111111111111111111111, which is -1. However, if we do:

然而,如果我们做的事:

System.out.println(-2 >>> 1); //prints 2147483647

since -2 = 11111111111111111111111111111110 and do an unsigned right shift, which means we shift by 1 bit with zero-extension, giving: 01111111111111111111111111111111

由于-2 = 111111111111111111111111111111111111111111111111111111111111111111 11111111111111,做一个无签名右移位,这意味着我们以零扩展的方式移动1位,给出:011111111111111111111111111111111111111111111111

#2


4  

The 32-bit decimal (two's complement) value -1 is 0xFFFFFFFF in hexadecimal. If you do an unsigned right shift ( >>>) by one bit on that you get 0x7FFFFFFF, which is 2147483647 decimal.

32位小数(2的补码)的值-1是十六进制的0xFFFFFFFF。如果你做一个无符号右移位(>>>)你会得到0x7FFFFFFF,它是2147483647十进制。

#3


2  

Your confusion arises from the (very common) misconception that there is a "sign bit" in 2s-complement intergers. It isn't. The left most bit, also known as the most significand bit (MSB) actively contributes to the value of the number.

您的困惑源于(非常常见的)误解,即在2 -补语中有一个“符号位”。它不是。最左的位,也被称为最重要的位(MSB),积极地为数字的价值做出贡献。

In 2s complement notation, this bit merely indicates the sign of the number. But manipulation of that bit does not simply change the sign.

在2s补充表示法中,这一点仅仅表示数字的符号。但对这一点的操控并不仅仅是改变标志。

Another noteworthy property of the machine internal int format is that you don't need to interpret them as signed numbers. In fact, this is exactly what you do when you use the >>> operator: You interpret the number as unsingned number (notwithstanding the myth that "Java does not have unsigned integers"). Hence:

机器内部int格式的另一个值得注意的属性是,您不需要将它们解释为带符号的数字。实际上,这正是使用>>>操作符时要做的事情:您将数字解释为未单数(尽管有“Java没有无无符号整数”的神话)。因此:

0xffffffff >>> 1 == 4294967295 / 2

and this is how your result makes sense. (Note that you can't write the above in Java source code, it will complain that the deciaml number is "out of range".)

这就是结果的意义。(注意,您不能用Java源代码编写上述代码,它会抱怨分贝数“超出范围”。)

Data types with true sign bits are IEEE floating point numbers.

带真符号位的数据类型是IEEE浮点数。

#4


1  

The left-most bit in signed integer values are used to indicate weather the number is positive (0) or negative (1). -1 is represented as all bits on: 11111111111111111111111111111111. If you shift it left with >>>, you get 01111111111111111111111111111111 which is the highest positive number 2^31 - 1 = 2147483647

符号整数值中最左边的位用来表示数字是正(0)还是负(1)。-1表示所有位在:11111111111111111111111111111111111111111111111111111111111111111111111111上。如果你把它留给> > >,你得到01111111111111111111111111111111的最高正数2 ^ 31 - 1 = 2147483647

#1


7  

The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

无符号右移位运算符>>>将0转换为最左位置,而“>>”后的最左位置依赖于符号的扩展。

So, -1 is shifted right one bit with zero-extension, which means it will insert a 0 into the leftmost position. Remember, we're dealing with two's complement here:

所以-1被右移了一个位,0延伸,这意味着它会把0插入到最左边的位置。记住,这里是2的补码

-1 is: 11111111111111111111111111111111 or 0xFFFFFFFF in Hex

-1是:11111111111111111111111111111111或十六进制中的0xffffff

-1 >>> 1 is 01111111111111111111111111111111 or 0x7FFFFFFF in Hex,
which is 231 - 1 == 2147483647

>> 1为0111111111111111111111111111111111111111111111111111111111111111111111111111或Hex中的0x7fffff,即231 -1 = 2147483647

Here's a JLS reference for shift operators.

这里是移位操作符的JLS引用。

You seemed to be confused about two's complement. 31 bits are used for the value and the bit farthest to the left is used for the sign. Since you're only shifting by 1 bit, the signed bit becomes a 0, which means positive, and the result is the greatest positive number than an int can represent.

你似乎对二的补数感到困惑。31位用于值,最左边的位用于符号。因为你只移动了1位,有符号的位变成了0,这意味着正的,结果是一个比int所能表示的最大的正数。

Perhaps another example will help. Let's consider the following:

也许另一个例子会有帮助。让我们考虑以下:

System.out.println(-2 >> 1); //prints -1

-2 = 11111111111111111111111111111110

2 = 11111111111111111111111111111110

If we use a signed right shift, we get: 11111111111111111111111111111111, which is -1. However, if we do:

然而,如果我们做的事:

System.out.println(-2 >>> 1); //prints 2147483647

since -2 = 11111111111111111111111111111110 and do an unsigned right shift, which means we shift by 1 bit with zero-extension, giving: 01111111111111111111111111111111

由于-2 = 111111111111111111111111111111111111111111111111111111111111111111 11111111111111,做一个无签名右移位,这意味着我们以零扩展的方式移动1位,给出:011111111111111111111111111111111111111111111111

#2


4  

The 32-bit decimal (two's complement) value -1 is 0xFFFFFFFF in hexadecimal. If you do an unsigned right shift ( >>>) by one bit on that you get 0x7FFFFFFF, which is 2147483647 decimal.

32位小数(2的补码)的值-1是十六进制的0xFFFFFFFF。如果你做一个无符号右移位(>>>)你会得到0x7FFFFFFF,它是2147483647十进制。

#3


2  

Your confusion arises from the (very common) misconception that there is a "sign bit" in 2s-complement intergers. It isn't. The left most bit, also known as the most significand bit (MSB) actively contributes to the value of the number.

您的困惑源于(非常常见的)误解,即在2 -补语中有一个“符号位”。它不是。最左的位,也被称为最重要的位(MSB),积极地为数字的价值做出贡献。

In 2s complement notation, this bit merely indicates the sign of the number. But manipulation of that bit does not simply change the sign.

在2s补充表示法中,这一点仅仅表示数字的符号。但对这一点的操控并不仅仅是改变标志。

Another noteworthy property of the machine internal int format is that you don't need to interpret them as signed numbers. In fact, this is exactly what you do when you use the >>> operator: You interpret the number as unsingned number (notwithstanding the myth that "Java does not have unsigned integers"). Hence:

机器内部int格式的另一个值得注意的属性是,您不需要将它们解释为带符号的数字。实际上,这正是使用>>>操作符时要做的事情:您将数字解释为未单数(尽管有“Java没有无无符号整数”的神话)。因此:

0xffffffff >>> 1 == 4294967295 / 2

and this is how your result makes sense. (Note that you can't write the above in Java source code, it will complain that the deciaml number is "out of range".)

这就是结果的意义。(注意,您不能用Java源代码编写上述代码,它会抱怨分贝数“超出范围”。)

Data types with true sign bits are IEEE floating point numbers.

带真符号位的数据类型是IEEE浮点数。

#4


1  

The left-most bit in signed integer values are used to indicate weather the number is positive (0) or negative (1). -1 is represented as all bits on: 11111111111111111111111111111111. If you shift it left with >>>, you get 01111111111111111111111111111111 which is the highest positive number 2^31 - 1 = 2147483647

符号整数值中最左边的位用来表示数字是正(0)还是负(1)。-1表示所有位在:11111111111111111111111111111111111111111111111111111111111111111111111111上。如果你把它留给> > >,你得到01111111111111111111111111111111的最高正数2 ^ 31 - 1 = 2147483647