Pages

Tuesday, June 16, 2015

Difference between Bitwise OR and Exclusive OR

Difference between Bitwise OR and Exclusive OR

Bitwise Or
  1. Takes 2 patterns of equal length and performs the logical inclusive OR operation
  2. forces a bit to be set ON in a sequence where corresponding mask bit is one
  3. Example
    1. 0101(5)
    2. 0011(3)
    3. 0111(7) // Output
Exclusive OR
  1. Returns XOR logical Exclusive OR outputs true when both inputs differ
  2. Truth Table
    1. 0 0 0
    2. 0 1 1
    3. 1 0 1
    4. 1 1 1

Swap 2 Numbers with Exclusive Or

Here is a simple program to swap to numbers with Exclusive OR

 public class Swap {  
   public static void main(String args[])  
   {  
     int a=4;  
     int b=5;  
     a^=b;  
     b^=a;  
     a^=b;  
     System.out.println(a);  
     System.out.println(b);  
   }  
 }  

Output :

5
4

Java - Division by Floating Zero

An amazing result of Java Program on Division by 0.0

 public class Fruits {  
   public static void main(String args[])  
   {  
     int numOranges=5;  
     int numApples=10;  
     double averageFruit=0.0;  
     double fruitTypes=0.0;  
     averageFruit=(numOranges+numApples)/fruitTypes;  
     System.out.println("Average fruit is "+averageFruit);  
   }  
 }  
 Output : Average fruit is Infinity  


Infinity means a positive but effectively infinite result that is greater than the largest number that can be stored in double