读书人

java中quot;lt;lt;quot;quot;gt;gt;quot;quot;gt;gt;gt;quot;等左移右移符号剖

发布时间: 2013-10-24 18:27:24 作者: rapoo

java中"<<",">>",">>>"等左移右移符号辨析

java中有以下移位运算规则:

"<<":带符号左移,向左移动n位,向低位添加0,如下图所示

public class Test {public static void main(String[] args) {byte a = -1;System.out.println(Integer.toBinaryString(a));a >>= 2;//a右移两位System.out.println(Integer.toBinaryString(a));short b = -1;System.out.println(Integer.toBinaryString(b));b >>>= 2;//a右移两位System.out.println(Integer.toBinaryString(b));}}/* * 结果如下:(都是按照int型处理) * 11111111111111111111111111111111 * 11111111111111111111111111111111 * 11111111111111111111111111111111 * 11111111111111111111111111111111 * */
如果使用long,结果则为long型,跟预期结果一致

读书人网 >编程

热点推荐