Abstract

The bitwise XOR expression is used to compute the bit-by-bit Boolean XOR (exclusive OR) of two integer values. 

Usage

The bitwise XOR expression is typically used to toggle the bits of an integer used as a bit field.

Syntax

Syntax Diagrams

Bitwise XOR Expression Syntax Diagram

BNF

bitwise-xor-expression
::= <bitwise-and-expression>
::= <bitwise-xor-additive-expression> <bitwise-xor-operator> <bitwise-and-expression>
bitwise-xor-operator
::= '^'

EBNF

bitwise-xor-expression
::= <bitwise-and-expression> ( <bitwise-xor-operator> <bitwise-and-expression> ) *
bitwise-xor-operator
::= '^'

Contextual Constraints

bitwise-xor-operator
The operands of the XOR operator should be of an integral type (int, byte, short, long, or char). 
If either operand is long, the other operand will be coerced to long.
If neither operand is long, the operands of the AND operator will be coerced to int type. 

Semantics

bitwise-xor-expression
For each bit position, the result is 0 iff the operands are equal in that bit position.
For each bit position, the result is 1 iff the operands are unequal in that bit position.
The value of each bit is the boolean XOR of that bit of the operands:
Boolean XOR
Operands Value
000
011
101
110
The value of the bitwise XOR operator is of type long if either operand is of type long.
The value of the bitwise XOR operator is of type int if neither operand is of type long.
The bitwise XOR operator uses full evaluation.  It does not short circuit. 

Example

class BitwiseXor {

  String Bits (int Value, int Width) {
    String P;
    String S;
    if (Value < 0) {
      if (Width < 32) {
        S = "**********************************";
        S = S.substring (0, Width);
      } else {
        Value = Integer.MAX_VALUE & Value;
        P = "0000000000000000000000000000000";
        S = Integer.toString (Value, 2);
        S = "1" + P.substring (1, 32 - S.length()) + S;
        if (Width > 32) {
          P = "                                ";
          S = P.substring (0, Width - 32) + S;
        }
      }
    } else {
      P = "                                ";
      S = Integer.toString (Value, 2);
      S = P.substring (0, Width - S.length()) + S;
      if (S.length () > Width) {
        S = "**********************************";
        S = S.substring (0, Width);
      }
    }
    return S;
  }
  
  String Decimal (int Value, int Width) {
    String P = "                      ";
    String S = Integer.toString (Value);;
    if (S.length () > Width) {
      S = "**********************************";
      S = S.substring (0, Width);
    } else if (S.length () < Width) {
      S = P.substring (0, Width - S.length()) + S;
    }
    return S;
  }
  
  void DisplayXOR (int A, int B) {
    int C;
    C = A ^ B;
    System.out.println ( "  " 
         + Decimal (A, 11) + "  " + Bits (A, 32));
    System.out.println ( "^ " 
         + Decimal (B, 11) + "  " + Bits (B, 32));
    System.out.println ( "  -----------  --------------------------------");
    System.out.println ( "= " 
         + Decimal (C, 11) + "  " + Bits (C, 32));
    System.out.println ( " "); 
  }

  public static void main (String[] Args) {
    BitwiseXor App;
    App = new BitwiseXor();
    App.DisplayXOR (5, 3);
    App.DisplayXOR (-5, -3);
    App.DisplayXOR (Integer.MAX_VALUE, Integer.MIN_VALUE );
  }

}

Output


    

Reference Links

Bitwise XOR Operator <bitwise-xor-operator>
Bitwise AND Expression <bitwise-and-expression>

External Links

Sun Java Integer Bitwise Operators The Java Language Specification, 3rd Edition
Sun Java Integer Bitwise Operators The Java Language Specification, 2nd Edition
Sun Java Integer Bitwise Operators The Java Language Specification, 1st Edition

Other Languages

C Now C Bitwise XOR Expression
C++ Now C++ Bitwise XOR Expression
Java Now
Java Bitwise XOR Expression
PHP Now PHP Bitwise XOR Expression