当前位置: 技术问答>java相关
请教一道SCJP题目的问题
来源: 互联网 发布时间:2017-04-07
本文导语: Which statements are true concerning the effect of the >> and >>> operators? 1) For non-negative values of the left operand, the >> and >>> operators will have the same effect. 2) The result of (-1 >> 1) is 0. 3) The result of (-1 >>> ...
Which statements are true concerning the effect of the >> and >>> operators?
1) For non-negative values of the left operand, the >> and >>> operators will have the same effect.
2) The result of (-1 >> 1) is 0.
3) The result of (-1 >>> 1) is -1.
4) The value returned by >>> will never be negative as long as the value of the right operand is equal to or greater than 1.
5) When using the >> operator, the leftmost bit of the bit representation of the resulting value will always be the same bit value as the leftmost bit of the bit representation of the left operand.
问题:
为什么第四个选项错误?
1) For non-negative values of the left operand, the >> and >>> operators will have the same effect.
2) The result of (-1 >> 1) is 0.
3) The result of (-1 >>> 1) is -1.
4) The value returned by >>> will never be negative as long as the value of the right operand is equal to or greater than 1.
5) When using the >> operator, the leftmost bit of the bit representation of the resulting value will always be the same bit value as the leftmost bit of the bit representation of the left operand.
问题:
为什么第四个选项错误?
|
你看看这两个输出的结果就知道了:
System.out.println(-1 >>> 31);// output 1
System.out.println(-1 >>> 32);// output -1
-1 >>> 0和-1 >>> 32(64,96……)一样;-1 >>> 1和-1 >>> 33(65,97……)一样……
所以第四个选项错误。
System.out.println(-1 >>> 31);// output 1
System.out.println(-1 >>> 32);// output -1
-1 >>> 0和-1 >>> 32(64,96……)一样;-1 >>> 1和-1 >>> 33(65,97……)一样……
所以第四个选项错误。
|
其实很简单,一个是有符号位移,一个是无符号位移
-1可以表示为二进制 1...........1
前面是1的表示是负的,如果不是负的就是0
自己推一下不就出来了
-1可以表示为二进制 1...........1
前面是1的表示是负的,如果不是负的就是0
自己推一下不就出来了