October 17, 1999 - The Bitwise XOR Operator
October 17, 1999 The Bitwise XOR Operator Tips: October 1999
Yehuda Shiran, Ph.D.
|
Bit1 Bit2 Bit1 ^ Bit2 0 0 0 0 1 1 1 0 1 1 1 0
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:
Name Decimal Hex Binary Op1 44 0x2C 0 0 1 0 1 1 0 0 Op2 163 0xA3 1 0 1 0 0 0 1 1 Op1 ^ Op2 143 0x8F 1 0 0 0 1 1 1 1