逻辑运算符以及位运算:
逻辑运算符:
//逻辑运算符
public class Demo05 {
public static void main(String[] args){
//和 ()and; 或(or); 非(不是你就是我)
boolean a = true;
boolean b = false;
System.out.print("a && b:"+(a && b));//逻辑 与运算:全真为真,一假则假
System.out.println("\n");
System.out.print("a || b:"+(a || b));//逻辑 或运算:全假为假,一真则真
System.out.println("\n");
System.out.print("! (a && b):"+(! (a && b)));//如果为真,则为假;如果为假,则为真
System.out.println("\n");
//短路运算
int c = 5;
boolean d = (c < 4) && (c++<4);
System.out.println(d);
System.out.println(c);
}
}
位运算:
// 位运算 (效率极高)
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
=========================================================
A & B: 0000 1100 (异为 0,同为同)
A | B: 0011 1101 (异为 1,同为同)
A ^ B (异或运算):0011 0001 (同为 0,异为 1)
~B (非 A即 B) :1111 0010 (跟 B 完全相反)
面试题:
2*8=16 2*2*2*2
<< : (左移 ,相当于 *2)
>> : (右移 ,相当于 /2)
0000 0000 0,
0000 0001 1,
0000 0010 2,
0000 0011 3,
0000 0100 4,
0000 0101 5,
0000 0110 6,
0000 0111 7,
0000 1000 8,
~~~~~~~~~~~~~~~
0001 0000 16,
*/
System.out.println(2<<3);
}
}