逻辑运算符、位运算符

逻辑运算符

package operator;

/**
 * @author WZT
 * @create 2021-03-24 10:01
 */
public class Demo05 {
    
    
    public static void main(String[] args) {
    
    
        //与(and)  或(or) 非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a&&b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a||b:"+(a||b));//逻辑或运算:两个变量有一个为真,则结果才为true
        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:)false
false
5

Process finished with exit code 0

位运算符

package operator;

/**
 * @author WZT
 * @create 2021-03-24 10:12
 */
public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
        /*
        A = 0011 1100
        B = 0000 1111
        ------------------------------------
        (与)A&B = 0000 1100
        (或)A|B = 0011 1111
       (异或) A^B = 0011 0011    相同为0,不相同为1
       (非) ~B = 1100 0000


          左移右移(对于乘除2的整数倍效率极高)
          <<  >>

        */
        System.out.println(2<<3);
    }
}

输出

16

猜你喜欢

转载自blog.csdn.net/weixin_45809838/article/details/115164157
今日推荐