逻辑运算符
// 逻辑运算符
public class Demo05 {public static void main(String[] args) {// 与(and) 或(or) 非(取反)boolean a = true;boolean b = false;// 逻辑与运算: 两个变量都为真,结果才为 trueSystem.out.println("a && b: " + (a&&b));// 逻辑或运算: 两个变量有一个为真,结果才为 trueSystem.out.println("a || b: " + (a||b));// 如果是真,则为假, 如果为假,则为真System.out.println("!(a && b): " + !(a&&b));// 短路运算int c = 5;// 前面为假,则后面就不执行了,所以C还是5boolean d = (c<4)&&(c++<4);System.out.println(d); // falseSystem.out.println(c); //5}
}
位运算符
public class Demo06 {public static void main(String[] args) {/*A = 0011 1100B = 0000 1101A&B = 0000 1100 都是1才是1 与A|B = 0011 1101 都是0才是0 或A^B = 0011 0001 相同就为0,不相同为1 异或运算~B = 1111 0010 取反2*8 = 16 2*2*2*2效率极高!!!<< *2>> /20000 0000 00000 0001 10000 0010 20000 0100 40000 1000 80001 0000 16*/// 2*8 2*2*2*2System.out.println(2<<3);}
}
https://www.bilibili.com/video/BV12J41137hu?p=29&spm_id_from=pageDriver