<?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: Leandro Alves Santos</title>
    <description>The latest articles on DEV Community by Leandro Alves Santos (@lealsdev).</description>
    <link>https://dev.to/lealsdev</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%2F481352%2Ffc1d2403-0bdc-4ad0-832d-19b7164f4591.jpeg</url>
      <title>DEV Community: Leandro Alves Santos</title>
      <link>https://dev.to/lealsdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lealsdev"/>
    <language>en</language>
    <item>
      <title>Swap Variable Values ​​Without a Temporary Variable</title>
      <dc:creator>Leandro Alves Santos</dc:creator>
      <pubDate>Sat, 03 Oct 2020 22:02:58 +0000</pubDate>
      <link>https://dev.to/lealsdev/swap-variable-values-without-a-temporary-variable-1hi7</link>
      <guid>https://dev.to/lealsdev/swap-variable-values-without-a-temporary-variable-1hi7</guid>
      <description>&lt;h4&gt;
  
  
  The problem
&lt;/h4&gt;

&lt;p&gt;One of the first problems that we learned in computer science is how to sort data. There are a lot of well known algorithms to achieve that.&lt;/p&gt;

&lt;p&gt;To solve this problem, one of the things that we must do is swap values within a collection.&lt;/p&gt;

&lt;p&gt;For the sake of simplicity, we will use the simplest sorting algorithm known as Bubble Sort.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

void swap(int* a, int* b) {

  int temp = *a;
  *a = *b;
  *b = temp;

  return;

}

void bubbleSort(int* arr, int size) {

  for(int i = 0; i &amp;lt; size; ++i) {

    for(int j = 0; j &amp;lt; size-i-1; ++j) {

      if(arr[j] &amp;gt; arr[j+1]) {
        swap(&amp;amp;arr[j], &amp;amp;arr[j+1]);
      }

    }

  }

  return;

}

int main(void) {

  int size = 8;

  int arr[8] = { 5, 4, 8, 7, 1, 2, 6, 3 };

  bubbleSort(arr, size);

  for(int i = 0; i &amp;lt; size; ++i) {
    printf("%d\n", arr[i]);
  }

  return 0;

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this code, we wrote a swap method that uses a temporary variable to support the swap operation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Bitwise XOR
&lt;/h4&gt;

&lt;p&gt;Bitwise XOR is an operation that returns true only when the operation is done with a true and false values, otherwise, the operation results into a false value.&lt;/p&gt;

&lt;p&gt;Note that this operation is done to each bit in the variable.&lt;/p&gt;

&lt;p&gt;Let's look at a XOR's table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;false&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;false&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With that in mind, let's look at an example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Input&lt;/td&gt;
&lt;td&gt;0 0 0 1 1 0 1 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Input&lt;/td&gt;
&lt;td&gt;0 1 0 1 1 1 0 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Result&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 1 0 0 0 1 1 1&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, let's rewrite our swap function using XOR operations. In C, the XOR operator is the ^.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void swap(int* a, int* b) {

  *a = *a ^ *b;
  *b = *a ^ *b;
  *a = *a ^ *b;

  return;

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's suppose that we are calling this method by passing the values 0001(1) and 0010(2).&lt;/p&gt;

&lt;p&gt;The first operation that happens is a = a ^ b&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0 0 0 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0 0 1 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;0 0 1 1&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The second operation that happens is b = a ^ b&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0 0 1 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0 0 1 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;0 0 0 1&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The third operation that happens is a = a ^ b&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0 0 1 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0 0 0 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;0 0 1 0&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This article showed that with bitwise XOR, we can swap the values ​​of the variables without using a third variable to support the operation.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>softwareengineering</category>
      <category>c</category>
    </item>
  </channel>
</rss>
