Difference between bitwise inclusive or and exclusive or in java

32,935

Solution 1

XOR tells whether each bit is different.

1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0

In other words "either but not both"

0011 XOR 0101 = 0110

Solution 2

BITWISE INCLUSIVE OR (|) means normal or operation ,

BITWISEE ExCLUSIVE OR (^) means xor operation

Solution 3

Third one is an XOR operation (Xclusive-OR)

It says, OR should be exclusively: where similar will be False(0) and dissimilar will be True(1).

So 12 in binary would be 1100

So, perform bitwise XOR here:

  1 1 0 0
  1 1 0 0
---------
  0 0 0 0
---------

Every column has same digit, either both are 1's or both are 0's XOR says, both should be different. Hence all zeros

Solution 4

Exclusive or (XOR) is defined as:

0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0

That is, it is 0 when two values are the same, 1 if they are different.

So, given two bit patterns which are exactly equal, each XORed bit will evaluate to 0, as each bit will either have 1 in both positions, or 0 in both positions.

Share:
32,935
PSR
Author by

PSR

Working as a java developer at CGI in HYDERABAD

Updated on April 17, 2020

Comments

  • PSR
    PSR about 4 years
    public class Operators {
    
        public static void main(String[] args) {        
            int a = 12;
    
        System.out.println("Bitwise AND:"+(12&12));
        System.out.println("Bitwise inclusive OR:"+(12|12));
        System.out.println("Bitwise exclusive OR:"+(12^12));
    
        }
    }
    
    OUTPUT:
    
    Bitwise AND:12
    Bitwise inclusive OR:12
    Bitwise exclusive OR:0
    

    I understand first two, but not the third.