Java logical operators

Java logical operators


AND OR NOT operation


public class Demo5 {
    
    
    public static void main(String[] args) {
    
    
        //与(and)    或(or)   非(取反)
        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));//逻辑非运算:如果为真,则为假。如果为假,则为真。

        //短路运算
        int c=1;
        boolean d=(c<0)&&(c++<0);//第一个就为假,所以后面的运算不参数
        System.out.println(d);
        System.out.println(c);

    }


}

Bit operation


public class Demo6 {
    
    
    public static void main(String[] args) {
    
    
        /*
        A=0011 1100
        b=0000 1101
        ----------------------------------------
        A&B=0000 1100   两个为1结果为1
        A|B=0011 1101   一个为1结果为1
        A^B=0011 0001   相同为0,不相同为1
        ~B=1111 0010    对B进行取反,0为1,1为0

        2*8=16  2*2*2*2
        效率提高
        <<  *2
        >>  /2
        0000 0000       0
        0000 0001       1
        0000 0010       2
        0000 0100       4
        0000 1000       8
        0001 0000       16
         */
        System.out.println(2<<3);
    }
}

Guess you like

Origin blog.csdn.net/qq_39453420/article/details/108181553