admin管理员组文章数量:1394217
In C, when there are 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 puted i^=j^=i^=j
, I have i = 5
, j = 3
.
However, if I use two int pointers to re-do this, with *i^=*j^=*i^=*j
, using the example above, what I have will be i = 0
and j = 3
.
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);
}
undefined behavior in c
Will the undefined behavior in c be the real reason leads to this question?
1
code piled use visual studio 2005 on windows 7 produce the expected result ( Output i = 7, j = 9 twice.)
2
code piled use gcc on ubuntu ( gcc test.c ) produce the unexpected result ( Output i = 7, j = 9 then i = 0, j = 9 )
3
code piled use gcc on ubuntu ( gcc -O test.c ) produce the expected result ( Output i = 7,j = 9 twice. )
In C, when there are 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 puted i^=j^=i^=j
, I have i = 5
, j = 3
.
However, if I use two int pointers to re-do this, with *i^=*j^=*i^=*j
, using the example above, what I have will be i = 0
and j = 3
.
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);
}
undefined behavior in c
Will the undefined behavior in c be the real reason leads to this question?
1
code piled use visual studio 2005 on windows 7 produce the expected result ( Output i = 7, j = 9 twice.)
2
code piled use gcc on ubuntu ( gcc test.c ) produce the unexpected result ( Output i = 7, j = 9 then i = 0, j = 9 )
3
code piled use gcc on ubuntu ( gcc -O test.c ) produce the expected result ( Output i = 7,j = 9 twice. )
Share Improve this question edited Aug 27, 2015 at 22:25 Tunaki 137k46 gold badges366 silver badges438 bronze badges asked Sep 8, 2012 at 14:16 klvoekklvoek 815 bronze badges 5- 2 Same idea: stackoverflow./questions/949433/… – Qaz Commented Sep 8, 2012 at 14:17
-
Does your code even pile? You are trying to dereference an
int
. – huon Commented Sep 8, 2012 at 14:21 -
3
Why bother?!
int temp = i; i = j; j = temp;
works every time. – Bo Persson Commented Sep 8, 2012 at 17:45 - Implement that simply is a better way also. But, I just curious about what was happened. – klvoek Commented Sep 9, 2012 at 15:08
- Regarding your edit: undefined behavior certainly can lead to the behavior you're seeing, since undefined behavior means that anything can happen (there is no 'expected' behavior for the piler). – Michael Burr Commented Sep 9, 2012 at 16:00
3 Answers
Reset to default 8i^=j^=i^=j
is undefined behavior in C.
You are violating sequence points rules by modifying i
two times between two sequence points.
It means the implementation is free to assign any value or even make your program crash.
For the same reason, *i^=*j^=*i^=*j
is also undefined behavior.
(C99, 6.5p2) "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression."
Consider the following code:
#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);
return 0;
}
If you try to pile it you will see a warning: unsequenced modification
for the first line. As @ouath said it's not well-defined. According to the C11 Standard the assignment-expressions work in a read-modify-write fashion. This operation isn't atomic on all the CPU architectures. You can read more about the warning here.
It's interesting to see that for *pi^=*pj^=*pi^=*pj;
there is no warning in my LLVM piler.
As for the 'more interesting' aspect added by the Javascript result:
While the expression is undefined in C as explained in ouah's answer, it is well-defined in Javascript. However the rules for how the expression is evaluated in Javascript might not be what you expect.
The ECMAscript spec says that a pound assignment operator is evaluated like so (ECMA-262 11.13.2):
The production AssignmentExpression : LeftHandSideExpression
@=
AssignmentExpression,where@
represents one of the operators indicated above, is evaluated as follows:
- Evaluate LeftHandSideExpression.
- Call GetValue(Result(1)).
- Evaluate AssignmentExpression.
- Call GetValue(Result(3)).
- Apply operator @ to Result(2) and Result(4).
- Call PutValue(Result(1), Result(5)).
- Return Result(5).
So the expression i ^= j ^= i ^= j
would be evaluated in the following steps:
i = (3 ^ (j ^= i ^= j))
i = (3 ^ (j = (5 ^ i ^= j)))
i = (3 ^ (j = (5 ^ (i = 3 ^ j)))))
i = (3 ^ (j = (5 ^ (i = 3 ^ 5)))))
i = (3 ^ (j = (5 ^ (i = 6)))))
i = (3 ^ (j = (5 ^ 6))))
i = (3 ^ (j = 3)) // j is set to 3
i = (3 ^ 3)
i = 0 // i is set to 0
Yet another reason to avoid the trick of using an xor operation to swap values.
本文标签: javascriptwhy ijij isn39t equal to *i*j*i*jStack Overflow
版权声明:本文标题:javascript - why i^=j^=i^=j isn't equal to *i^=*j^=*i^=*j - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744769627a2624259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论