The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java xor operator with example programs

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java xor operator with example programs Posted: May 6, 2017 7:31 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java xor operator with example programs
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

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

Read: Java xor operator with example programs

Topic: How to check if a character is a special character in java Previous Topic   Next Topic Topic: Java Tutorial: Enum in java | Java enum - Level

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use