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

What is the importance of XOR operator in Java?

王林
Release: 2023-09-07 08:53:07
forward
1446 people have browsed it

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);
   }
}
Copy after login

Output

false XOR false: false
false XOR true: true
true XOR false: true
true XOR true: false
Copy after login

##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);
   }
}
Copy after login

Output

0100100000
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template