>> =和>>> =之间的差异

时间:2014-06-25 20:40:17

标签: java

我想找出>> =和>>> =之间的区别。我明白他们做了什么,但我不明白其中的区别。 以下输出为38 152 38 152。 逐位分配>>> =似乎与>> =完全相同。

public static void main(String[] args)
{
    int c = 153;
    System.out.print((c >>= 2));
    System.out.print((c <<= 2));
    System.out.print((c >>>= 2));
    System.out.print((c <<= 2));
}

1 个答案:

答案 0 :(得分:6)

详细了解Bitwise and Bit Shift Operators

>>      Signed right shift
>>>     Unsigned right shift

位模式由左侧操作数给出,位置数由右侧操作数移位。无符号右移运算符>>> 移至最左侧位置

>>之后的最左侧位置取决于符号扩展名。

简单地说>>>总是移到最左边的位置,而>>根据数字的符号移位,即1为负数数字和0表示正数。


例如尝试使用负数。

int c = -153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.println(Integer.toBinaryString(c <<= 2));

c = 153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));

输出:

11111111111111111111111111011001
11111111111111111111111101100100
  111111111111111111111111011001
11111111111111111111111101100100
                          100110
                        10011000
                          100110
                        10011000
相关问题