java 左移<< 与右移 >>

针对正整数

<<左移

按二进制形式把所有的数字向左移动对应的位数,低位的空位补零。

>>右移

按二进制形式把所有的数字向右移动对应位移位数,低位移出(舍弃),最高位则移入原来高位的值。

举例:如:a = 00110111,则a>>2=00001101,b=11010011,则b>>2 = 11110100

 

    public static void main(String[] args) {
        int a=53;
        System.out.println("53右移2位"+(a>>2));   // 相当于53/(2*2)
        System.out.println("53左移1位"+(a<<1));   // 相当于53*(2)

    }

result:

猜你喜欢

转载自blog.csdn.net/Growing_hacker/article/details/108529775