The type of each of the operands of a shift operator must be a primitive integral type, or a compile-time error occurs. Binary numeric promotion (§5.6.2) is not performed on the operands; rather, unary numeric promotion (§5.6.1) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand. If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive. 我也没看懂为什么要用一个5位/6位的mask,哈哈哈哈哈
x>>y 当X为int时,Y可以大于32,例如: class Test { public static void main(String[] args) { int i=32; System.out.println(""+(i>>>33)); } }输出:16 表明>>>32等于>>>1 同样<<,>>Y都可超过32
The type of each of the operands of a shift operator must be a primitive integral type, or a compile-time error occurs. Binary numeric promotion (§5.6.2) is not performed on the operands; rather, unary numeric promotion (§5.6.1) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.
If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive.
我也没看懂为什么要用一个5位/6位的mask,哈哈哈哈哈
这主要是防止程序员写错程序,Y太大,导致cpu处理时间过长。所以执行上面的语句时,
只用Y的低5位(最大32)或低6位(最大64)。明白了吗?见java2认证学习指南(英文原版)第37、38页。
当X为int时,Y可以大于32,例如:
class Test {
public static void main(String[] args) {
int i=32;
System.out.println(""+(i>>>33));
}
}输出:16
表明>>>32等于>>>1
同样<<,>>Y都可超过32