Home  >  Article  >  Java  >  What is the importance of XOR operator in Java?

What is the importance of XOR operator in Java?

王林
王林forward
2023-09-07 08:53:071438browse

What is the importance of XOR operator in Java?

Bit XOR (exclusive or) "^" is an operator in Java. If the two bits in the operand are different, ## is returned. #'1', if the two bits are the same, the XOR operator returns the result '0'. XOR is a binary operator that evaluates from left to . The operator "^" is undefined for arguments of type String. Example

public class XORTest1 {
   public static void main(String[] args) {
      boolean x = false;
      boolean y = false;
      boolean xXorY = x ^ y;
      System.out.println("false XOR false: "+xXorY);
      x = false;
      y = true;
      xXorY = x ^ y;
      System.out.println("false XOR true: "+xXorY);
      x = true;
      y = false;
      xXorY = x ^ y;
      System.out.println("true XOR false: "+xXorY);
      x = true;
      y = true;
      xXorY = x ^ y;
      System.out.println("true XOR true: "+xXorY);
   }
}

Output

false XOR false: false
false XOR true: true
true XOR false: true
true XOR true: false

##Example

public class XORTest2 {
   public static void main(String[] args) {
      String str1 = "1010100101";
      String str2 = "1110000101";
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < str1.length(); i++) {
         sb.append(str1.charAt(i)^str2.charAt(i));
      }
      System.out.println(sb);
   }
}
Output

0100100000

The above is the detailed content of What is the importance of XOR operator in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete