Logical XOR (Operator/logical)

Logically exclusive OR of two values.

There isn't really an XOR logical operator, but the Bitwise XOR operator should work fine.

This is proven by evaluating the truth table in the example.

This seems to work fine even on MSIE where the sign bit exhibits some instability under Bitwise operations.

This is the truth table for two Boolean primitive values being operated on with the XOR operator.

ABXOR
falsefalsefalse
falsetruetrue
truefalsetrue
truetruefalse

Example code:

   <HTML>
   <HEAD></HEAD>
   <BODY>
   <TABLE BORDER=1 CELLPADDING=2>
   <TR>
   <TH>A</TH>
   <TH>B</TH>
   <TH>XOR</TH>
   </TR>
   <SCRIPT>
   for(a=0; a<2; a++)
   {
      for(b=0; b<2; b++)
      {
         document.write("<TR ALIGN=CENTER><TD>");
         document.write(Boolean(a));
         document.write("</TD><TD>");
         document.write(Boolean(b));
         document.write("</TD><TD>");
         document.write(Boolean(a ^ b));
         document.write("</TD></TR>");
      }
   }
   </SCRIPT>
   </TABLE>
   </BODY>
   </HTML>

See also:Bitwise XOR (^)