Difference between bitwise inclusion or exclusive or in java

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 the first two, but not the third.

+8
java operators bitwise-operators
source share
5 answers

XOR indicates 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 not both"

0011 XOR 0101 = 0110

+16
source share

BITWISE INCLUSIVE OR (|) means normal operation or ,

BITWISEE ExCLUSIVE OR (^) means operation xor

+4
source share

Third - XOR operation (Xclusive-OR)

It says: OR should be exclusive: where it will be False (0) in the same way and different from Truth (1).

So 12 in binary will be 1100

So do the bitwise XOR here:

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

Each column has the same digit, both are equal to 1 or both are equal to 0 XOR says that both should be different. Hence all zeros

+3
source share

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 that the two bit patterns are exactly equal, each XORed bit will be evaluated as 0 , since each bit will either have 1 in both positions, or 0 in both positions.

+1
source share

since the operators you use are bitwise operators, so the operands are converted to bits, and then the operator acts on them.

here 12 β†’ 1100 in binary digits (bit)

and the operations of the operators (A), | (OR) and ^ (Exclusive OR or XOR) are as follows:

AB and | ^

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

so when you do 12 ^ 12:

A = 1100

B = 1100

Ab ^

1 1 0

1 1 0

0 0 0

0 0 0

and 0000 β†’ 0 in decimal

hence you get 12 ^ 12 = 0 in your third answer

-2
source share

All Articles