How does bitwise ^ (XOR) work?
XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).
XOR table:
| X | Y | X^Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example: 4^3 = 7
1In binary:
2 0100
3 ^ 0011
4 ------
5 Result: 0111 => (7)XOR with negative numbers
Let's understand with an example -4^-2 = 2
In the above example, we can see -4^-2 output will be 2
but the question arises how? Because if we represent both inputs in the binary form, then we do XOR of bits, then the output will be 0000 0110 and in decimal, it will be 6 but as we know output should be 2.
11000 0100 (-4)
2^ 1000 0010 (-2)
3------------------
4 0000 0110 => (6) // incorrect outputNote: Here, the leftmost bit position is reserved for the sign of the value (positive or negative) and doesn't contribute towards the value of the number.
Let's understand how the XOR operation works with negative numbers.
How XOR operation works with negative numbers?
First, the XOR operation is to XOR each bit (the same is 0, the difference is 1), but you need to convert the number into a complement first.
-
The complement of a positive number is itself
-
The complement of the negative number is reversed for each bit and then incremented by 1 (the highest is kept at 1)
1// Lets take -4
2In binary: 1000 0100
3Reverse: 1111 1011
4complement (Increment by 1): 1111 1100
5// Now -2
6In binary: 1000 0010
7Reverse: 1111 1101
8complement (Increment by 1): 1111 1110
9Final result:
10complement of -4 : 1111 1100
11complement of -2 : 1111 1110
12 -----------
13Result: 0000 0010 => 2
14</code></pre>
15<p><em>Here, the MSB bit of result will denote the sign, and the rest of the bits will denote the value of the final result.</em>
16XOR sign table could be useful to understand the sign of result:</p>
17<table>
18<thead>
19<tr>
20<th>X</th>
21<th>Y</th>
22<th>X^Y</th>
23</tr>
24</thead>
25<tbody>
26<tr>
27<td>+</td>
28<td>+</td>
29<td>+</td>
30</tr>
31<tr>
32<td>+</td>
33<td>-</td>
34<td>-</td>
35</tr>
36<tr>
37<td>-</td>
38<td>+</td>
39<td>-</td>
40</tr>
41<tr>
42<td>-</td>
43<td>-</td>
44<td>+</td>
45</tr>
46</tbody>
47</table>
48<p>The above approach will work with negative inputs, but if we have positive and negative, then?</p>
49<h6>How XOR operation works with positive and negative numbers?</h6>
50<p>Let's performs <code>-5 ^ 2</code>
51Follow the same above approach to get a complement code of <code>-5</code>.</p>
52<pre><code class="language-javascript">complement of -5 : 1111 1011
53// Note: complement of a positive number is itself
54complement of 2 : 0000 0010
55complement result: 1111 1001
56
57Now, complement result -1 : 1111 1000
58 -----------
59Final result(inverse code): 1000 0111 => -7
60</code></pre>
61<h5>Some interesting use cases of XOR operator</h5>
62<ul>
63<li>
64<p>Swap two number without the third variable
65Example:</p>
66<pre><code class="language-javascript">function swapWithXOR (a, b) {
67 a = a^b;
68 b = a^b;
69 a=a^b;
70 console.log("result => a: " + a + ", b: " + b)
71}
72
73swapWithXOR(4, 1); // result => a: 1, b: 4
74</code></pre>
75</li>
76<li>
77<p>RAID Drive Backups (Systems)</p>
78</li>
79<li>
80<p>Cryptography (XOR cipher)</p>
81</li>
82<li>
83<p>Array operations</p>
84</li>
85</ul>
