<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rahul Parshi</title>
    <description>The latest articles on DEV Community by Rahul Parshi (@parshirahul).</description>
    <link>https://dev.to/parshirahul</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F123920%2Ffc5b356b-287d-453b-b283-a0a85226a360.jpg</url>
      <title>DEV Community: Rahul Parshi</title>
      <link>https://dev.to/parshirahul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/parshirahul"/>
    <language>en</language>
    <item>
      <title>Swapping 2 variables(numbers, characters, strings, booleans) without using extra variable</title>
      <dc:creator>Rahul Parshi</dc:creator>
      <pubDate>Sun, 14 Feb 2021 10:58:11 +0000</pubDate>
      <link>https://dev.to/parshirahul/swapping-2-variables-numbers-characters-strings-booleans-without-using-extra-variable-146k</link>
      <guid>https://dev.to/parshirahul/swapping-2-variables-numbers-characters-strings-booleans-without-using-extra-variable-146k</guid>
      <description>&lt;h2&gt;
  
  
  Numbers
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Using arithmetic operation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        a = a + b;
        b = a - b;      // (a+b) - (b) = a
        a = a - b;      // (a+b) - (a) = b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using binary operation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        a = a ^ b;
        b = a ^ b;      // (a ^ b) ^ (b) = a ^ 0 = a 
        a = a ^ b;      // (a ^ b) ^ (a) = 0 ^ b = b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Characters
&lt;/h2&gt;

&lt;p&gt;If characters are stored with it's ASCII value like C++, same method as numbers can be used. Else characters can be type caste to numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        a = (char) ((int)a ^ (int)b);
        b = (char) ((int)a ^ (int)b);    
        a = (char) ((int)a ^ (int)b); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Strings
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        a = a + b;      
        b = a.substr(0, (a.length() - b.length()));   
        a = a.substr(b.length(), a.length());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Space complexity = S(M + N) where M is length of a and N is length of b;&lt;/p&gt;

&lt;h2&gt;
  
  
  Booleans
&lt;/h2&gt;

&lt;p&gt;If both the variables are same, swapping is not required else need to toggle the values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        if (a != b){
            a = true ^ a;   //toggles the value
            b = true ^ b;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>firstpost</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
