相关疑难解决方法(0)

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

对于以下程序:

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);
  }
Run Code Online (Sandbox Code Playgroud)

我得到如下输出:

x = -1
y = 2147483647
Run Code Online (Sandbox Code Playgroud)

我得到的结果-1>>>1是2147483647.如果它是必须移位的符号位,正如我所知,结果应该是1073741824.为什么它是2147483647呢?

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

样本图片

java bit-shift

12
推荐指数
1
解决办法
864
查看次数

标签 统计

bit-shift ×1

java ×1