class Test
{
public static void main(String args[])
{
int i=-3;
System.out.println(Integer.toBinaryString(i<<1));
System.out.println(Integer.toBinaryString(i*2));
System.out.println(Integer.toBinaryString(i>>1));
System.out.println(Integer.toBinaryString(i/2));
}
}

解决方案 »

  1.   

    System.out.println(Integer.toBinaryString(i>>1));
    System.out.println(Integer.toBinaryString(i/2));但上面的结果不一样呀,(i/2)  不就是除2取整吗?
      

  2.   

    对于负数,是不一样的。System.out.println(Integer.toBinaryString(-3));
    System.out.println(Integer.toBinaryString(-3>>1));
    System.out.println(Integer.toBinaryString(-3/2));
    System.out.println(Integer.toBinaryString(-1));输出是:11111111111111111111111111111101
    11111111111111111111111111111110
    11111111111111111111111111111111
    11111111111111111111111111111111发现问题没有?
    第一行输出是-3
    第二行输出是-3右移一位,由于是负数,高位补1
    第四行输出是-1
    而第三行输出跟第四行输出一样,说明什么?这就说明是输出的是-(3/2),即-1明白?
      

  3.   

    《Java Language Specification》中对>>的描述如下:The value of n>>s is n right-shifted s bit positions with sign-extension. The resulting value is &#8970;n/2s&#8971;. For nonnegative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s.也就是说,对于非负数n,右移s位与n除以2的s次方等价。对于负数而言,JLS中并没有说明。
    参见:http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.19
      

  4.   

    根据JLS的说法还有上面的例子,应该是这样。