why i^=j^=i^=j isn't equal to *i^=*j^=*i^=*j
- by klvoek
In c , when there is variables (assume both as int) i less than j , we can use the equation
i^=j^=i^=j
to exchange the value of the two variables. For example, let int i = 3, j = 5; after computed i^=j^=i^=j, I got i = 5, j = 3 . What is so amazing to me. But, if i use two int pointers to re-do this , with *i^=*j^=*i^=*j , use the example above what i got will be i = 0 and j = 3. Then, describe it simply:
In C
1
int i=3, j=5;
i^=j^=i^=j; // after this i = 5, j=3
2
int i = 3, j= 5;
int *pi = &i, *pj = &j;
*pi^=*pj^=*pi^=*pj; // after this, $pi = 0, *pj = 5
In JavaScript
var i=3, j=5;
i^=j^=i^=j; // after this, i = 0, j= 3
the result in JavaScript makes this more interesting to me
my sample code , on ubuntu server 11.0 & gcc
#include <stdio.h>
int main(){
int i=7, j=9;
int *pi=&i, *pj=&j;
i^=j^=i^=j;
printf("i=%d j=%d\n", i, j);
i=7, j==9;
*pi^=*pj^=*pi^=*pj
printf("i=%d j=%d\n", *pi, *pj);
}
however, i had spent hours to test and find out why, but nothing means. So, please help me. Or, just only i made some mistake???