day13-逻辑运算符、位运算符

逻辑运算符

package operator;

//逻辑运算
public class Demo04 {
public static void main(String[] args) {
//与(and) 或(or) 非(not取反)
boolean a = true;
boolean b = false;

    System.out.println("a && b:"+(a&&b));//逻辑与运算:两个变量为真,结果为true,否则为false
    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(c);
    System.out.println(d);
}

}

位运算符

public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
逻辑运算——位层次
A&B=0000 1100 与运算(AND):有两个输入,如果输入都是1,则输出1;其他三种情况,输出都是0
A|B=0011 1101 或运算(OR):有两个输入,如果输入都是0,则为0;其他三种情况,输出都是1
A^B=0011 0001 异或运算(XOR):有两个输入,如果输入相同,则输出0;如果输入不同,则输出1
~B=1111 0010 非运算(NOT):只有一个输入,输出位是输入位的相反;

   2*8=16   2*2*2*   位运算:效率极高
   <<:左移 *2   >>:右移 /2
     */
    System.out.println(2<<3);
}

}

posted @ 2021-06-29 21:48  shum  阅读(60)  评论(0)    收藏  举报