Javascript - Operator Bitwise XOR

Introduction

The bitwise XOR operator is ^ and works on two values.

Here is the truth table for bitwise XOR:

Bit From First Number Bit From Second Number Result
1 1 0
1 0 1
0 1 1
0 0 0

Bitwise XOR returns 1 only when exactly one bit has a value of 1. If both bits contain 1, it returns 0.

The following code uses XOR on the numbers 25 and 3:

var result = 25 ^ 3; 
console.log(result);    //26 

Here is how it worked.

25 = 0000 0000 0000 0000 0000 0000 0001 1001 
 3 = 0000 0000 0000 0000 0000 0000 0000 0011 
--------------------------------------------- 
XOR = 0000 0000 0000 0000 0000 0000 0001 1010 

Related Topic