逻辑运算符,移位运算符

package operator;

/**
 * @author xyf
 * @create 2020-08-10-20:33
 */
public class Domo4 {
    public static void main(String[] args) {
        /**
         逻辑与运算 &&
         逻辑或运算 ||
         逻辑非运算
         */
        boolean a=true;
        boolean b=false;
        System.out.println("a&&b:"+(a&&b));
        System.out.println("a||b:"+(a||b));
        System.out.println("!(a&&b):"+!(a&&b));
        System.out.println("!(a||b):"+!(a||b));
        //短路运算
        int c=5;
        boolean d=(c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);

    }
}

输出

a&&b:false
a||b:true
!(a&&b):true
!(a||b):false
false
5

 

package operator;

/**
 * @author xyf
 * @create 2020-08-10-20:41
 */
public class Demo05 {
    public static void main(String[] args) {
        /**
         A=0011 1100
         B=0000 1101
         A&B=0000 1100
         A|B=0011 1101
         A^B=0011 0001 异或运算
         ~b=1111 0010 取反
         */

        /**2*8如何最快
         << 左移动一位 相当于*2一次
         >>右移动一位   相当于/2一次
         0000 0000=0
         0000 0001=1
         0000 0010=2
         0000 0011=3
         0000 0100=4
         0000 1000=8
         0001 0000=16
        */
        System.out.println(2<<3);//位运算,效率极高
        System.out.println(3<<3);
        System.out.println(8>>2);

    }
}

输出

16
24
2

 

你可能感兴趣的:(逻辑运算符,移位运算符)