Form

<exclusive-or-operator>
^

Semantics

^
name: Exclusive OR (bitwise XOR)
side effect: None.
associativity: Left to right.   (The bitwise XOR operation is associative.)
commutativity: The bitwise XOR operation is commutative.
note: The Bitwise XOR operation does not short circuit.

Integer Operands

^ (^: int × int → int)
operand types: int
result value: Each bit of the result is the boolean XOR of the corresponding bits of the operors.
result type: int
note: Operands of byte and short type are cast to int type.
^ (^: long × long → long)
operand types: long
result value: Each bit of the result is the boolean XOR of the corresponding bits of the operors.
result type: long
note: One operand of byte, short, or int type is cast to long type.

Remarks

Bitwise OR
Operands Result
0 0 0
0 1 1
1 0 1
1 1 0

Example

class BitwiseXor {

  void Display (byte A, byte B) {
    System.out.println ( "byte:      " 
         + (int)A + " ^ " + (int)B 
         + " = " + (A ^ B));
    System.out.println ( "short:     " 
         + (int)A + " ^ " + (int)B + " = " 
         + ((short)A ^ (short)B));
    System.out.println (  "int:       " 
         + (int)A + " ^ " + (int)B + " = " 
         + ((int)A ^ (int)B));
    System.out.println (  "long:      " 
         + (long)A + " ^ " + (long)B + " = " 
         + ((long)A ^ (long)B));
  }

  static void main (String[] Args) {
    BitwiseXor App;
    App = new BitwiseXor();
    App.Display ((byte)7, (byte)-2);
    App.Display ((byte)-7, (byte)-2);
  }

}

Output

byte:      7 ^ -2 = -7
short:     7 ^ -2 = -7
int:       7 ^ -2 = -7
long:      7 ^ -2 = -7
byte:      -7 ^ -2 = 7
short:     -7 ^ -2 = 7
int:       -7 ^ -2 = 7
long:      -7 ^ -2 = 7

See Also

    Summary
  <boolean-and-operator>   &&
  <boolean-or-operator>   ||
  <bitwise-and-operator>   &
  <bitwise-or-operator>   |
Exclusive Or Operator
  <exclusive-or-operator>   ^

Valid HTML 4.01 Transitional
Valid HTML 4.01 Transitional

Site Icon Copyright © 1999-2006, jhyoung, revised 11/16/06