1. ## swap integer problem

today i practised selection sorting algorithm with following

Code:
```#include <stdio.h>
#include <stdlib.h>

#define LEN 10

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

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

*/
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}

void selection_sort(int *arr, int len)
{
int i = 0;
for(;i<len-1;i++)
{
int j=i+1,min=i;
for(;j<len;j++)
{
if(arr[j] < arr[min])
{
min = j;
}
}
swap(&arr[min],&arr[i]);
}
}

int main()
{
int *a = malloc(sizeof(int) * LEN);
int i = 0;

for(;i<LEN;i++)
{
a[i] = rand()%300;
printf("%d\t",a[i]);
}
printf("\n");

selection_sort(a,LEN);

for(i=0;i<LEN;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
free(a);
}```

if i use code block
int temp = *a;
*a = *b;
*b = temp;

csxia-laptop:/data/learning/C/DataStructure # ./selection_insert
283 286 177 115 293 235 286 192 249 121
115 121 177 192 235 249 283 286 286 293

it works fine , but if i use the latter code block
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
then a and b's value will get zero if the address of a and b is the same

csxia-laptop:/data/learning/C/DataStructure # ./selection_insert
283 286 177 115 293 235 286 192 249 121
115 121 0 192 235 249 283 0 286 293

i have no idea why, anyone who can help me???

2. Originally Posted by csxia
it works fine , but if i use the latter code block
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
then a and b's value will get zero if the address of a and b is the same

i have no idea why, anyone who can help me???
Think about what happens when you write:
Code:
```int x = 2;
x = x ^ x;```

3. Originally Posted by csxia
but if i use the latter code block
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
then a and b's value will get zero if the address of a and b is the same
i have no idea why, anyone who can help me???
It's because XOR of equal numbers is 0.

4. OMG this is a bug