» » Java xor operator with example programs


Java Bitwise XOR operator:

  • Java bit wise Xor operator operates on bits of numbers.
  • It will be represented as "^".
  • XOR will return true if  both bits are different.
  •  For example 1 XOR 0 gives 1. Ex: 1^0=1.
  • 0^1=1.




Java Bitwise Xor:

Java bitwise XOR operator


Program#1:Java Example program on two integer numbers Xor operation.


  1. package com.Xorinjava;
  2. public class XOR {
  3.     /**
  4.     * Xor in java Xor example program
  5.     * @author www.instanceofjava.com
  6.     */
  7.     public static void main(String[] args) {
  8.         int a=1;
  9.         int b=0;
  10.         int c= a^b;
  11.         
  12.         System.out.println(c);
  13.  
  14.     }
  15.  
  16. }
Output:

  1. 1

Program #2: java xor boolean expression : java example program


  1. package com.Xorinjava;
  2.  
  3. public class XOR {
  4.     /**
  5.     * Xor in java boolean Xor example program
  6.     * @author www.instanceofjava.com
  7.     */
  8. public static void main(String[] args) {
  9.         boolean a=true;
  10.         boolean b=false;
  11.         boolean c= a^b;
  12.         
  13.         System.out.println(c);
  14.         
  15.         System.out.println(false^false);
  16.         System.out.println(true^true);
  17.  
  18.     }
  19.  
  20. }

Output:


  1. true
  2. false
  3. false
Program #3: java xor boolean expression and integer : java example program Using Eclipse IDE.

java Xor operator

Java Xor Strings:

  • Gives compile time error.
  • The operator ^ is undefined for the argument type(s) java.lang.String, java.lang.String

Program #4: Java Xor of two strings


  1. package com.Xorinjava;
  2. public class XOR {
  3.     /**
  4.     * Xor in java boolean Xor example program
  5.     * @author www.instanceofjava.com
  6.     */
  7.     public static void main(String[] args) {
  8.         String str="a";
  9.         String str1="b";
  10.         System.out.println(str^str1);// compile time error: The operator ^ is undefined for the
  11. argument type(s) java.lang.String, java.lang.String
  12.  
  13.     }
  14.  
  15. }


 Xor of Binary Strings:

Program #5: Java xor of two binary strings.


  1. package com.Xorinjava;
  2. public class XOR {
  3.     /**
  4.     * Xor in java String bits Xor example program
  5.     * @author www.instanceofjava.com
  6.     */
  7.     public static void main(String[] args) {
  8.         String str1="1010100101";
  9.         String str2="1110000101";
  10.         StringBuffer sb=new StringBuffer();
  11.         
  12.         for (int i = 0; i < str1.length(); i++) {
  13.             
  14.             sb.append(str1.charAt(i)^str2.charAt(i));
  15.             
  16.         }
  17.        System.out.println(sb);
  18.     }
  19.  
  20. }
Output:


  1. 0100100000

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu