Why swap with xor works fine in c++ but in java doesn't ? some puzzle [duplicate]
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Why is this statement not working in java x ^= y ^= x ^= y; Sample code int a=3; int b=4; a^=(b^=(a^=b)); In c++ it swaps variables, but in java we get a=0, b=4 why? 回答1: By writing your swap all in one statement, you are relying on side effects of the inner a^=b expression relative to the outer a^=(...) expression. Your Java and C++ compilers are doing things differently. In order to do the xor swap properly,