✔ 最佳答案
Can I answer this in English?
2009-07-01 06:59:15 補充:
This is an old trick used to swap two integers fast. Note that the operator ==, +=, -=, ^= ... are all evaluated from right to left because their associativity is from right to left. I have rewrote your program into the following for ease of explaination:
line # code
1 x
2 =
3 ( y
4 +=
5 x
6 -=
7 y)
8 -
9 x
Now, due to the fact that = on line 2 has right associativity, (y+=x-=y)-x has to be evaluated first. Again, += has the same issue. Thus, x-=y has to be evaluted first. Also, -= will have the same order of evaluation. Therefore, y has to be evaluated first.
Now, y at line# 7, at this moment has a value of 6. Therefore, x-=6 will give a value of 3 and assigned to x at line #5.
Afterwards, y+=3 will be evaluated and y at line# 3, will be assigned a value of 9.
At the end, x at line #1, will be assigned a value of 9-3 which is 6.
I used to use is:
y^=x^=y^=x;
This is not used anymore in real code due to the advance in compiler technology (I was told).