Java foundation -3.3

Bit operation
means that binary data can be calculated directly, mainly: | (!) And (&), or (), NOT, XOR (^), the inverted (-), the shift process (>> , <<)

  • AND operation
public class xxx {
    public static void main(String[] args) {
        int x = 7; //00000111
        int y = 13;//00001101
        System.out.println(x & y);
    }
}
>>>5//00000101

1 only two are the result of a 1 is 0 instead of 1 results

  • OR
public class xxx {
    public static void main(String[] args) {
        int x = 7; //00000111
        int y = 13;//00001101
        System.out.println(x | y);
    }
}
>>>15//00001111

Two as long as there is a 1 1 only two are 0 0

  • Shift operation
public class xxx {
    public static void main(String[] args) {
        int x = 2; //00000010
        System.out.println(x<<2);
        System.out.println(x);
    }
}
>>>8//00001000
>>>2//00000010 

First << 2 left two large (multiply by 2 twice)
to the right by two small >> 2 (divided by two divided by two)

Will & and &&, | || the difference between?

  • And & | two operators can bits and logical operations;
    • During bit operation and the time or only with data for the current process;
    • Performing a logic operation when all of the conditions judged to be executed;
  • In the logical operation may also be used &&, ||;
    • &&: determining when a number of conditions, if the previous condition returns false, the conditions are no longer all subsequent final determination result is false;
    • ||: determining when a number of conditions, if the previous condition returns true, the subsequent condition is not performed, is the final true;
    public class xxx {
      public static void main(String[] args) {
          int i =2;
          if(true&false&(i++>0)) {
              System.out.println(1);
          }
          System.out.println(i);
          if(false&&(i++>0)) {
              System.out.println(2);
          }
          System.out.println(i);
      }
    }
    >>>3
    >>>3
    Two conditions are false, but the value of i is output from the determination for all visible & determination condition as long as the foregoing conditions && returns false determination condition is no longer follow the natural or 3 instead of 4 i

Guess you like

Origin www.cnblogs.com/sakura579/p/12307329.html