The first method is also a commonly used method. The exchange is completed through multiple numerical calculations. The following three methods have been known:
(1) addition and subtraction.
A = A + B;
B = A-B;
A = A-B;
This method can exchange integer and floating-point values, but may cause loss of precision when processing floating-point values, for example:
A = 3.123456
B = 1234567.000000
The value of each variable changes:
A = 1234567.000000
B = 3.125000
Obviously, the original value of A has a loss of precision in the process of switching to B.
(2) multiplication and division.
A = A * B;
B = A/B;
A = A/B;
Multiplication and division are more like the ing from addition and subtraction to multiplication and division operations. They are similar to addition and subtraction: They can process integer and floating-point variables, but there is also a problem of precision loss when dealing with floating-point variables. In addition, multiplication and division have one more constraint than addition and subtraction: B must not be 0.
Some kind of experience tells us that addition and subtraction and multiplication and division may overflow, and multiplication and division overflow may be very serious. Otherwise, neither of the two methods will overflow. Taking addition and subtraction as an example, the first addition operation may cause overflow, but the overflow caused by this operation will be overflow in the subsequent subtraction operation.
(3) exception or method.
A ^ = B; // A = a ^ B
B ^ = A; // B = B ^ (a ^ B) = B ^ A ^ B = B ^ A = 0 ^ A =
A ^ = B; // A = (a ^ B) ^ A = a ^ B ^ A = a ^ A ^ B = 0 ^ B = B
The XOR method can exchange integer variables, but it cannot exchange floating-point variables.
The second method is more like playing a text game. This method uses the method of embedding assembly code in the code to avoid the introduction of temporary variables, however, in essence, additional storage space will be used. There are many ways to use this method, which are listed below:
(1) using the xchg command is also an intuitive and easy-to-think method, because the function of the xchg command is to exchange the value of the Source and Destination operands, use Additional registers to store variables. The embedded assembly code is as follows:
_ ASM
{
MoV eax,
Xchg B, eax
MoV A, eax
}
(2) Use additional stacks. Here, the reverse order of the stack is used to complete the exchange. Embedded code can be in the following two forms:
_ ASM
{
Push
Push B
Pop
Pop B
}
Another form:
_ ASM push
A = B;
_ ASM pop
(3) Use the mov command. This method uses Additional registers to store the value of a variable.
_ ASM mov eax,
A = B;
_ ASM mov B, eax
In fact, the second method is not qualified. Although it does not explicitly use temporary variables, it still uses additional storage space. However, it cannot be said that there is no need to master it. from a practical perspective, it is still "useful. Didn't a company issue this interview question? "Do not use addition, subtraction, or XOR to complete the process. Do not use the intermediate variable to exchange the values of two numeric variables ". In this case, you may have to use this method.