<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sumit Trivedi on Medium]]></title>
        <description><![CDATA[Stories by Sumit Trivedi on Medium]]></description>
        <link>https://medium.com/@emailtrivedi?source=rss-ef8bdfe7f3b7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*QOi5t88ypuPKRBgl</url>
            <title>Stories by Sumit Trivedi on Medium</title>
            <link>https://medium.com/@emailtrivedi?source=rss-ef8bdfe7f3b7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:03:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@emailtrivedi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Practical Applications of XOR in Programming]]></title>
            <link>https://medium.com/@emailtrivedi/practical-applications-of-xor-in-programming-f4c047b230ec?source=rss-ef8bdfe7f3b7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4c047b230ec</guid>
            <dc:creator><![CDATA[Sumit Trivedi]]></dc:creator>
            <pubDate>Sat, 06 Jul 2024 04:13:30 GMT</pubDate>
            <atom:updated>2024-07-06T04:13:30.257Z</atom:updated>
            <content:encoded><![CDATA[<p>The XOR (Exclusive OR) operation is a fundamental bitwise operator in computer science. It returns true (1) if and only if one of the operands is true (1), but not both. Otherwise, it returns false (0). This simple yet powerful operation has several properties and applications in algorithms.</p><figure><img alt="xor gate truth table and diagram" src="https://cdn-images-1.medium.com/max/1024/1*o8KA_dIZctt6kG9JqTAUUg.png" /></figure><h3>Properties of XOR</h3><blockquote>Identity: a⊕ 0 = a<br> XORing any number with 0 leaves the number unchanged.<br><br>Self-Inverse: a⊕ a = 0<br> XORing a number with itself results in 0.<br><br>Associative: (a ⊕ b)⊕ c = a⊕ (b ⊕ c)<br> The order of operations does not affect the result.<br><br>Commutative: a⊕ b = b ⊕ a<br> The order of operands does not affect the result.</blockquote><h3>Applications of XOR</h3><blockquote><strong>1. Swapping two variables without a temporary variable</strong></blockquote><p>Using XOR, you can swap two variables without needing an extra storage variable. This method leverages the self-inverse and commutative properties of XOR.</p><pre>a = a ^ b # Step 1: XOR a and b, store result in a<br>b = a ^ b # Step 2: XOR result with b, store result in b<br>a = a ^ b # Step 3: XOR result with a, store result in a<br># Now, a and b are swappedLets understand this by an example<em>.</em></pre><p>Lets understand this by an example. Assume you have two variables, a and b, that you want to swap without using a temporary variable.</p><p>a = 5 (binary 0101)</p><p>b = 3 (binary 0011)</p><p>Step 1 <strong>: </strong><em>XOR </em><em>a with </em><em>b and store the result in </em><em>a</em></p><pre>a = a ^ b  # Now, a = 5 ^ 3 (binary `0101 ^ 0011 = 0110`), so a = 6<br><br>a becomes 6 (binary 0110), b remains 3 (binary 0011).</pre><p>Step 2 <strong>: </strong><em>XOR </em><em>a (now 6) with </em><em>b and store the result in </em><em>b</em></p><pre>b = a ^ b  # Now, b = 6 ^ 3 (binary `0110 ^ 0011 = 0101`), so b = 5<br><br>b becomes 5 (binary 0101), a remains 6 (binary 0110).</pre><p>Step 3<strong> : </strong><em>XOR </em><em>a (still 6) with </em><em>b (now 5) and store the result in </em><em>a</em></p><pre>a = a ^ b  # Now, a = 6 ^ 5 (binary `0110 ^ 0101 = 0011`), so a = 3<br><br>a becomes 3 (binary 0011), b remains 5 (binary 0101).</pre><p>Final Values:</p><ul><li>a is now 3</li><li>b is now 5</li></ul><p><em>Why It Works:</em></p><ul><li>The XOR operation combines the bits of the numbers in a way that retains enough information to reverse the process and retrieve the original values.</li><li>The self-inverse property of XOR (a ^ a = 0) ensures that applying XOR with the same value twice cancels out the original value.</li><li>The identity property of XOR (a ^ 0 = a) ensures that XORing with 0 leaves the number unchanged.</li></ul><h4>When Not to Use XOR for Swapping Variables</h4><ol><li><em>Same Variable:</em></li></ol><ul><li>If a and b refer to the same variable, XOR swapping will set the variable to 0. For example:</li></ul><pre>a = a ^ a  # a becomes 0 <br>a = a ^ a  # a remains 0 <br>a = a ^ a  # a remains 0</pre><p><em>2. Floating Point Numbers:</em></p><ul><li>XOR swapping only works with integers. Using XOR on floating point numbers is not valid and can lead to unexpected results or errors.</li></ul><blockquote><strong>2. Finding the single missing element in a sequence</strong></blockquote><p>Given a sequence of numbers from 1 to n with one missing number, XOR can help identify the missing number.</p><pre># XOR of numbers from 1 to n<br>a = 1 ^ 2 ^ … ^ n<br><br># XOR of given sequence with one number missing<br>b = sequence_xor<br><br># The missing number<br>missing_number = a ^ b</pre><p><em>How it works </em>: XORing all numbers from 1 to n with the given sequence cancels out all pairs, leaving the missing number.</p><blockquote><strong>3. Finding the single odd occurring number</strong></blockquote><p>In a sequence where one number appears an odd number of times and all others appear an even number of times, XOR can identify the odd-occurring number.</p><pre>sequence = [1, 2, 2, 1, 3, 4, 5, 5, 4]<br>odd_occurring_number = 0<br>for num in sequence:<br>   odd_occurring_number ^= num<br><br># odd_occurring_number is 3</pre><p><em>Why it works</em> : XORing a number an even number of times results in 0 due to the self-inverse property. XORing any number with 0 returns the number itself because of the identity property, thereby isolating the odd-occurring number.</p><blockquote><strong>4. Utilizing the self inverse property in encryption</strong></blockquote><p>The self-inverse property of XOR is used in simple encryption algorithms.</p><pre># Encryption<br>cipher_text = plain_text ^ key<br><br># Decryption<br>decrypted_text = cipher_text ^ key</pre><p><em>How it works</em> : XORing the plain text with a key produces the cipher text. XORing the cipher text with the same key utilizes the self-inverse property to cancel out the key, resulting in plain_text XOR 0, and then the identity property ensures this simplifies to the original plain text.</p><h3>Conclusion</h3><p>The XOR operation is a versatile tool in algorithm design, providing efficient solutions for swapping variables, finding missing elements, identifying odd-occurring numbers, and simple encryption. Understanding its properties and applications can greatly enhance your problem-solving skills in computer science and programming.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4c047b230ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>