7.4-位运算符

~ 位反    

&    位与运算

|    位或运算

^    位异运算    即1 0或0 1为true,0 0或1 1为flase

>>    有符号右位移    x>>a    x右移a位,高位用符号补齐

<<    有符号左位移    x<

>>>  无符号右位移    x>>>a  x不带符号右移a位,高位用0补齐

&= 即 a&=b, a=a&b

|=   即 a|=b, a=a|b

^=  即 a^=b, a=a^b

>>    <<    >>>同理

1、有符号的右位移相当于当前数除以2的n次方

2、左位移n,相当于当前数乘以2的n次方

3、无符号右位移,会导致数据不可靠


public class HelloWorld {

    public static void main(String[] args) {

        byte a = 0B00110010; //十进制50

        byte b = 0B01011110; //十进制94

        System.out.println("a | b = " + (a | b)); // 0B01111110

        System.out.println("a & b = " + (a & b)); // 0B00010010

        System.out.println("a ^ b = " + (a ^ b)); // 0B01101100

        System.out.println("~b = " + (~b)); // 0B10100001

        System.out.println("a >> 2 = " + (a >> 2)); // 0B00001100

        System.out.println("a >> 1 = " + (a >> 1)); // 0B00011001

        System.out.println("a >>> 2 = " + (a >>> 2)); // 0B00001100

        System.out.println("a << 2 = " + (a << 2)); // 0B11001000

        System.out.println("a << 1 = " + (a << 1)); // 0B01100100

        int c = -12;

        System.out.println("c >>> 2 = " + (c >>> 2));

        System.out.println("c >> 2 = " + (c >> 2));

}

}

你可能感兴趣的:(7.4-位运算符)