Comparative study on the variable exchange of common brain remnant we all know, get a temp variable temp does the relay, stores the value of a variable, and finally supplies another variable.
temp = i;
i = j;
j = temp;
There are also two kinds of variable exchange methods, not temp.
Note: This is about the function-encapsulated type.
func (int *a,int*b);
int i = 5;
int j = 6;
Func (&I,&J);
The first type is addition:
i = 5;
j = 6;
Inside the function:
*a = *a + *B;//11
*b = *a-*B;//5
*a = *a-*B;//6
Outside of the function, I becomes 6,J variable 5.
The second is the Xor method:
i = 5;
j = 6;
Inside the function:
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
Outside of the function, I becomes 6,J variable 5.
Convert to binary:
I:0101
j:0110
First time XOR:
i:0011
Second time XOR:
J:0101
Third-time XOR:
i:0110
Just swapping.
But there are limitations to these two types of exchanges:
This exchange, the use of the scene without loops, the loop has to judge the boundary, so inevitably the variables themselves and their own exchange situation.
themselves and their situation:
int i = 5;
int *j = &i;
Func (&i,&i);
Error is mainly in the address error, although no one will use the Func (&i,&i) to exchange, but sometimes the reference is more, visual can not see also normal
Func (&I,J);
equivalent to
i = i + I;//10
i = i-i;//0
i = i-i;//0
is not on the spot on the crazy force.
XOR is not pushed, because it is different or, the result is of course directly zero.
However, it does not mean that the two-variable equal interchange result should be zero.
i = 5;
j = 5;
i = i + J;//10
j = I-J;//5
i = I-J;//5
XOR:
i = 5;
j = 5;
i = i ^ j;//0000
j = i ^ j;//0101
i = i ^ j;//0101
The fundamental difference is that the latter is two variables, occupies two addresses, two addresses can save two values, memory function, you can achieve the purpose of the exchange. and a variable, any operation will change itself, no memory function, so can not complete the exchange.
Ways to avoid:
1. Check the cycle boundaries: avoid more loops, but also charge your brain.
2. Either subscript or address comparison before swapping: this is simple.
3. Or simply do not: But, since we all use, or do not toss back it, or not white toss a. After all, there are some topics that require no space overhead (like a lot of flipping strings).
4. Since it is provided in the form of functions, the function encapsulates the test, and the direct detection value is equal, the return is equal, and no exchange is required. This also covers the same address situation.
Or, the better approach is--pointer detection. The former is only used on the evasion, pointer detection is the function of the robustness of the internal detection.
Func (int *a, int *b)
{
if (a== b)
Return
Do swap
}
20170727 Amendment Supplement: The Operation premise is not emphasized before is the function encapsulation and the pointer, so I and I, I and I exchange is also difficult to understand and will not happen, and now the exchange logic is encapsulated with a function, and the use of pointers to pass parameters, to really exchange external variables, but also good to reflect the problems that will arise, Finally, with the pointer check, it is a more rigorous specification of the amendment.