. . . This can be achieved in three ways
1. Addition and subtraction
For example
a=a+b
b=a-b
p>
a=a-b
Of course, this method is not very good because it may cause a loss of accuracy. . . .
For example, a = 3.123456 b = 1234567.000000
After exchange, the values ??of each variable become:
a = 1234567.000000 b = 3.125000
So It is said that it is suitable for exchanging variables with integer and floating-point values
2. Multiplication and division
a = a * b;b = a / b;a = a / b;
Multiplication and division are more like the mapping of addition and subtraction to multiplication and division. It is similar to addition and subtraction: it can handle integer and floating-point variables, but there is also a loss of precision when handling floating-point variables. question. But multiplication and division have a little more requirements--b must not be 0.
We can see from the above: addition, subtraction, multiplication and division may overflow, and the overflow of multiplication and division will be particularly serious. In fact, otherwise, there will be no overflow using these two methods. Taking addition and subtraction as an example, the addition operation in the first step may cause overflow, but the overflow caused by it will be overflowed in the subsequent subtraction operation.
3. XOR method
a ^= b;b ^= a;a ^= b; The XOR method can complete the exchange of integer variables, for floating point types Variable it cannot complete the exchange.
So these three methods each have their own uses. You have to choose according to your own situation. . . . .