{"componentChunkName":"component---src-templates-tag-js","path":"/tags/xor/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":1,"edges":[{"node":{"fields":{"slug":"/engineering/how-does-bitwise-xor-work/"},"html":"<h2 id=\"how-does-bitwise--xor-work\" style=\"position:relative;\"><a href=\"#how-does-bitwise--xor-work\" aria-label=\"how does bitwise  xor work permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How does bitwise ^ (XOR) work?</h2>\n<p>XOR is a bitwise operator, and it stands for \"exclusive or.\" It performs <strong>logical</strong> operation. If input bits are the same, then the output will be false(0) else true(1).</p>\n<p>XOR table:</p>\n<table>\n<thead>\n<tr>\n<th>X</th>\n<th>Y</th>\n<th>X^Y</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>0</td>\n<td>0</td>\n<td>0</td>\n</tr>\n<tr>\n<td>0</td>\n<td>1</td>\n<td>1</td>\n</tr>\n<tr>\n<td>1</td>\n<td>0</td>\n<td>1</td>\n</tr>\n<tr>\n<td>1</td>\n<td>1</td>\n<td>0</td>\n</tr>\n</tbody>\n</table>\n<p>Example:  <code>4^3 = 7</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">In</span><span class=\"mtk1\"> binary: </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t     </span><span class=\"mtk7\">0100</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t   ^ </span><span class=\"mtk7\">0011</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t    ------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> Result: </span><span class=\"mtk7\">0111</span><span class=\"mtk1\">  </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">7</span><span class=\"mtk1\">)</span></span></code></pre>\n<h4 id=\"xor-with-negative-numbers\" style=\"position:relative;\"><a href=\"#xor-with-negative-numbers\" aria-label=\"xor with negative numbers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>XOR with negative numbers</h4>\n<p>Let's understand with an example <code>-4^-2 = 2</code>\nIn the above example, we can see <code>-4^-2</code>output will be <code>2</code>\nbut 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 <code>0000 0110</code> and in decimal, it will be <code>6</code> but as we know output should be <code>2</code>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0100</span><span class=\"mtk1\"> (-</span><span class=\"mtk7\">4</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">^ </span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0010</span><span class=\"mtk1\"> (-</span><span class=\"mtk7\">2</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">------------------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk7\">0000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0110</span><span class=\"mtk1\"> </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> (</span><span class=\"mtk7\">6</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// incorrect output</span></span></code></pre>\n<blockquote>\n<p>Note: Here, the leftmost bit position is reserved for the sign of the\nvalue (positive or negative) and doesn't contribute towards the value of the number.</p>\n</blockquote>\n<p> Let's understand how the XOR operation works with negative numbers.</p>\n<h5 id=\"how-xor-operation-works-with-negative-numbers\" style=\"position:relative;\"><a href=\"#how-xor-operation-works-with-negative-numbers\" aria-label=\"how xor operation works with negative numbers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How XOR operation works with negative numbers?</h5>\n<p>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.</p>\n<ol>\n<li>The complement of a positive number is itself</li>\n<li>\n<p>The complement of the negative number is reversed for each bit and then incremented by 1 (the highest is kept at 1)</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">// Lets take -4</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">In</span><span class=\"mtk1\"> binary: \t\t\t</span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0100</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">Reverse: \t\t\t</span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1011</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">complement</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">Increment</span><span class=\"mtk1\"> </span><span class=\"mtk12\">by</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1</span><span class=\"mtk1\">): \t</span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1100</span></span></code></pre>\n</li>\n</ol>\n<p>// Now -2\nIn binary: \t\t\t1000 0010\nReverse: \t\t\t1111 1101\ncomplement (Increment by 1): \t1111 1110</p>\n<p>Final result:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">4</span><span class=\"mtk1\"> : </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1100</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">2</span><span class=\"mtk1\"> : </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1110</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                  -----------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">Result:            </span><span class=\"mtk7\">0000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0010</span><span class=\"mtk1\"> \t</span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> </span><span class=\"mtk7\">2</span></span></code></pre>\n<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>\nXOR sign table could be useful to understand the sign of result:</p>\n<table>\n<thead>\n<tr>\n<th>X</th>\n<th>Y</th>\n<th>X^Y</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>+</td>\n<td>+</td>\n<td>+</td>\n</tr>\n<tr>\n<td>+</td>\n<td>-</td>\n<td>-</td>\n</tr>\n<tr>\n<td>-</td>\n<td>+</td>\n<td>-</td>\n</tr>\n<tr>\n<td>-</td>\n<td>-</td>\n<td>+</td>\n</tr>\n</tbody>\n</table>\n<p>The above approach will work with negative inputs, but if we have positive and negative, then? </p>\n<h5 id=\"how-xor-operation-works-with-positive-and-negative-numbers\" style=\"position:relative;\"><a href=\"#how-xor-operation-works-with-positive-and-negative-numbers\" aria-label=\"how xor operation works with positive and negative numbers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How XOR operation works with positive and negative numbers?</h5>\n<p>Let's performs <code>-5 ^ 2</code>\nFollow the same above approach to get a complement code of <code>-5</code>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">5</span><span class=\"mtk1\"> :          </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1011</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">// Note: complement of a positive number is itself</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk4\">of</span><span class=\"mtk1\"> </span><span class=\"mtk7\">2</span><span class=\"mtk1\"> :           </span><span class=\"mtk7\">0000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0010</span><span class=\"mtk1\">  </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">complement</span><span class=\"mtk1\"> result:          </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1001</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Now</span><span class=\"mtk1\">, </span><span class=\"mtk12\">complement</span><span class=\"mtk1\"> </span><span class=\"mtk12\">result</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">1</span><span class=\"mtk1\"> : </span><span class=\"mtk7\">1111</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1000</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                           -----------</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Final</span><span class=\"mtk1\"> </span><span class=\"mtk11\">result</span><span class=\"mtk1\">(</span><span class=\"mtk12\">inverse</span><span class=\"mtk1\"> </span><span class=\"mtk12\">code</span><span class=\"mtk1\">): </span><span class=\"mtk7\">1000</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0111</span><span class=\"mtk1\">   </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> -</span><span class=\"mtk7\">7</span></span></code></pre>\n<h4 id=\"some-interesting-use-cases-of-xor-operator\" style=\"position:relative;\"><a href=\"#some-interesting-use-cases-of-xor-operator\" aria-label=\"some interesting use cases of xor operator permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Some interesting use cases of XOR operator</h4>\n<ul>\n<li>\n<p>Swap two number without the third variable\nExample:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">swapWithXOR</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">a</span><span class=\"mtk1\">, </span><span class=\"mtk12\">b</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">a</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">a</span><span class=\"mtk1\">^</span><span class=\"mtk12\">b</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">b</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">a</span><span class=\"mtk1\">^</span><span class=\"mtk12\">b</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">a</span><span class=\"mtk1\">=</span><span class=\"mtk12\">a</span><span class=\"mtk1\">^</span><span class=\"mtk12\">b</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;result =&gt; a: &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">a</span><span class=\"mtk1\"> + </span><span class=\"mtk8\">&quot;, b: &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">b</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">swapWithXOR</span><span class=\"mtk1\">(</span><span class=\"mtk7\">4</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1</span><span class=\"mtk1\">); </span><span class=\"mtk3\">// result =&gt; a: 1, b: 4</span></span></code></pre>\n</li>\n<li>RAID Drive Backups (Systems)</li>\n<li>Cryptography (XOR cipher)</li>\n<li>Array operations</li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n</style>","frontmatter":{"date":"November 24, 2020","updated_date":null,"title":"How does bitwise ^ (XOR) work?","tags":["bitwise","XOR"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/3dc791a9bfecb4dc03ef8263118ccf7d/58556/xor.webp","srcSet":"/static/3dc791a9bfecb4dc03ef8263118ccf7d/61e93/xor.webp 200w,\n/static/3dc791a9bfecb4dc03ef8263118ccf7d/1f5c5/xor.webp 400w,\n/static/3dc791a9bfecb4dc03ef8263118ccf7d/58556/xor.webp 800w,\n/static/3dc791a9bfecb4dc03ef8263118ccf7d/99238/xor.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Kheenvraj Lomror","github":"rajlomror","avatar":null}}}}]}},"pageContext":{"tag":"XOR"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}