Reputation: 40318
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.
Upvotes: 11
Views: 35316
Reputation: 1775
BITWISE INCLUSIVE OR (|) means normal or operation ,
BITWISEE ExCLUSIVE OR (^) means xor operation
Upvotes: 11
Reputation: 582
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
Upvotes: 3
Reputation: 26040
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.
Upvotes: 1
Reputation: 3225
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
Upvotes: 20