October 17, 1999 - The Bitwise XOR Operator | WebReference

October 17, 1999 - The Bitwise XOR Operator

Yehuda Shiran October 17, 1999
The Bitwise XOR Operator
Tips: October 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

The bitwise XOR (exclusive OR) operator compares two bits and assign 1 to the result when the two operands are different. Here is the truth table for this operator:

Bit1Bit2Bit1 ^ Bit2
000
011
101
110

The XOR operator, like all other bitwise operators, can take only numeric values as its operands. Since JavaScript does not support bit-length variables, you have to do your bitwise math with byte-length operands, i.e. with integers that are less than 256. Any operand longer than a byte (greater than 255) will be chopped and only the LSB (Least Significant Byte) will be used.

Let's do a simple calculation:

NameDecimalHexBinary
Op1440x2C00101100
Op21630xA310100011
Op1 ^ Op21430x8F10001111