Assume that there are two numbers X and Y:
Method 1: arithmetic operation (addition or subtraction ):
X = x + y; // y = x-y for temporary storage. // y is the sum of the two numbers Minus Y, that is, the original xx = x-y; // X is the sum of the two numbers minus the current y (the original X) to change to the original y
Method 2: logical operation (exclusive or ):
X ^ = y; // Save the information of X and Y first. y ^ = x; // keep X unchanged, use X to exclusive or reverse the original value of Y to make it equal to the original value of x ^ = y; // keep Y unchanged, use X to exclusive or reverse the original value of Y to make it equal to the original value of Y.
Two principles should be kept in mind for exclusive or operations:
Any binary number with the same 1 variance or will change to another (0 with the same 1 variance or the result is with the same 1 variance or the result is 0)
The binary number of any digit is the same as 0 or both remain unchanged (0 is the same as 0 or 0 is the same as 0, and the result is 1)