Shifts can be combined with the equal sign (<<= or >>= or >>>=). The lvalue is replaced by 
the lvalue shifted by the rvalue. There is a problem, however, with the unsigned right shift 
combined with assignment. If you use it with byte or short, you don’t get the correct results. 
Instead, these are promoted to int and right shifted, but then truncated as they are assigned 
back into their variables, so you get -1 in those cases
. The following example demonstrates 
this:  红色部分怎么理解?谢谢

解决方案 »

  1.   

    运算的时候提升为int,运算结束赋值的时候又把高位截掉了.
      

  2.   

    java没有为byte,short等定义运算,它们的加减乘除以及位运算都是提升为int类型进行的,计算结果是int类型.
      

  3.   

    so you get -1 in those cases????????
    为什么会得到-1,还有个问题啊,高位是左边还是右边???
      

  4.   

    public class Test4 {
    public static void main(String[] args) {
    byte b=-2;
    System.out.println(Integer.toBinaryString(b));
    b>>>=10;
    System.out.println(Integer.toBinaryString(b));
    }
    }result:
    11111111111111111111111111111110
    11111111111111111111111111111111
    我不理解的是,为什么不管b是多少,b>>>=10之后都为-1???????
    或者是报的错?
      

  5.   

    直接看的话高位在左,从右边开始数是第0位.
    不知道这里作者指的in those cases到底是什么.
    具体结果肯定还与原来的值以及移动的位数有关系,写个例子就测试出来了.
      

  6.   

     byte b =5 ;
            System.out.println(Integer.toBinaryString(b));
            b>>>=3;
            System.out.println(Integer.toBinaryString(b));
            System.out.println(b);
      

  7.   

     byte b =-5 ;
            System.out.println(Integer.toBinaryString(b));
            b>>>=2;
            System.out.println(Integer.toBinaryString(b));
            System.out.println(b);