邏輯運算子

银行脱坑人發表於2024-08-20

邏輯運算子

&& || !

package operator;

/**
 * @version: java version 1.8
 * @Author: Mr Theroux
 * @description:
 * @date: 2024-08-20 9:33
 */
public class Demo05 {
    public static void main(String[] args) {
        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));//!反

    }
}

& | ^ ~ << >>

package operator;

/**
 * @version: java version 1.8
 * @Author: Mr Theroux
 * @description:
 * @date: 2024-08-20 10:00
 */
public class Demo06 {
 public static void main(String[] args) {
  /*
  A = 0011 1100;
  B = 0000 1101;
  A&B = 0000 1100;
  A|B = 0011 1101;
  A^B = 0011 0001; 位置相同則為0,否則為1
  ~B = 1111 0010;

  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 1000   8
   */
  System.out.println(2<<3);
 }
}

相關文章