This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

The operands 2 and 5 are not being added, although addition in this case produces the same result, which may be confusing you. The operands are the individual bits that represent each number in binary notation, not decimal notation.

Here's another example 2^3=1.

The ^ XOR operator works at the bit level, so convert the decimal numbers to binary: 2 = 0010 3 = 0011 ^ = 0001

You are thinking of a single-bit, exclusive-or operation:

0 EXOR 0 is 0 0 EXOR 1 is 1 1 EXOR 0 is 1 1 EXOR 1 is 0

But the Java ^ operation on integral types is a bitwise operation. The operation defined above is applied to all the bits of the ^ operands, in parallel, as the previous poster demonstrated.

Also note than ^ is defined for booleans, and does want you wish: the result is true iff the operand have different boolean values:

false ^ false is false false ^ true is true true ^ false is true true ^ true is false

But then again, you get the same result with boolean's == operation.