逻辑运算符、位运算符

逻辑运算符

逻辑运算符: &&与(and) 、||或(or)、!非(not)

短路运算 :可以提前判定结果的情况下不再继续执行后面的内容

位运算符

位运算符: &、|、~、^、<<、>> ;关于计算机的二进制底层; 效率极高

代码

package com.zhan.operator;

public class Test09 {
    public static void main(String []args){
        // 逻辑运算符: &&与(and) 、||或(or)、!非(not)
        boolean a=true;
        boolean b=false;
        System.out.println("a && b : " + (a&&b));    // && 逻辑与运算   ;   这里 + 号 表示将要输出的东西连接起来
        System.out.println("a || b : " + (a||b));    // || 逻辑或运算
        System.out.println("!a : " + (!a));          // ! 逻辑非运算

        //短路运算 :可以提前判定结果的情况下不再继续执行后面的内容
        int i=3;
        int n=3;
        boolean flag1=(i>10) &&(i++<10);   //前假后真,但可以提前判定结果的情况下不再继续执行后面的内容
        boolean flag2=(n++>2) &&(n++<5);   //前后都真,要执行完后面的才能判断
        System.out.println(flag1);
        System.out.println(flag2);
        System.out.println(i);
        System.out.println(n);

        System.out.println("============================");

        //位运算: &、|、~、^、<<、>>  ;关于计算机的二进制底层; 效率极高
        int x=0b1001_0010;
        int y=0b0011_0110;
        System.out.println(x&y); // &  与            18=0b 0001_0010
        System.out.println(x|y); // |  或
        System.out.println(x^y); // ^  异或
        System.out.println(~x);  // ~  非
        System.out.println(1<<3);  //左移 :*2 :1*2*2*2=1*2^3=8
        System.out.println(16>>3);  //右移 :/2 :16/2/2/2=16/(2^3)=2
    }
}