It is often used to exchange values of two variables when learning programming languages and designing programs. Generally, we define a new variable and use it for exchange. The Code is as follows:
int a,b,t;
a=10; b=15;
t=a; a=b; b=t;
This algorithm is easy to understand and is especially suitable for beginners to understand the characteristics of computer programs. It is a classic Application of Value assignment statements. In actual software development, this algorithm is simple and clear, and does not produce ambiguity. It facilitates communication between programmers. In general, you may encounter the problem of switching variable values, this algorithm should be used (hereinafter referred to as the standard algorithm ).
A temporary variable is needed. Can I exchange data without using temporary variables? The answer is yes! Here we can use at least three algorithms:
1) arithmetic operations
2) pointer operation
3) bitwise operation
1) arithmetic operations
In short, it is implemented through the + and-operations. The Code is as follows:
int a,b;
a=10;b=12;
a=b-a; //a=2;b=12
b=b-a; //a=2;b=10
a=b+a; //a=12;b=10
Through the above calculation, the values in a and B are exchanged. It looks simple on the surface, but it is not easy to think of, especially after getting used to standard algorithms.
Compared with the standard algorithm, this algorithm has three more computing processes, but does not use temporary variables. (Hereinafter referred to as an arithmetic algorithm)
2) pointer operation
The pointer operation is actually an integer operation. For example, two int pointers subtract to get an integer N, which indicates that the storage location of the two pointer variables in the memory is separated by N * sizeof (int) bytes; the int pointer is added to an integer, for example, "a + 10" indicates the int variable at 10 * sizeof (int) at the base address of. Therefore, we can complete the exchange of pointer variable values through operations similar to arithmetic algorithms, so as to exchange variables. That is:
int *a,*b;
A = new int (10); // assign a value to the pointer
b=new int(20); //a=0x00030828,b=0x00030840
a=(int*)(b-a); //a=0x00000006
b=(int*)(b-int(a)); //b=0x00030828
a=(int*)(b+int(a)); //a=0x00030840
Note: In the last three sentences, only the first sentence is the calculation between two pointers, and the other is the calculation of pointers and integers. Otherwise, the calculation will be wrong and the system will seriously fail.
After the preceding operations, the addresses of a and B are switched. a points to the original B and B points to the original a point!
This algorithm does not use the third variable to complete the value exchange. It is not easy to understand compared with the arithmetic algorithm, but it has the advantage that when switching a large data type, for example, a custom large structure or class performs faster than an arithmetic algorithm. Because of the address it exchanges, the variable value is not moved in the memory. (Hereinafter referred to as address algorithm)
3) bitwise operation
Variable exchange can also be achieved through exclusive or operations, which is perhaps the most amazing. Please refer to the following code:
int a=10,b=12; //a=1010^b=1100;
a=a^b; //a=0110^b=1100;
b=a^b; //a=0110^b=1010;
a=a^b; //a=1100=12;b=1010;
The implementation of this algorithm is determined by the characteristics of the exclusive or operation. Through the exclusive or operation, some bits in the data can be flipped while others remain unchanged. This means that a number a and any given number B are two consecutive times, and the value of a remains unchanged. That is, a ^ B =. If a = a ^ B is substituted into B = a ^ B, B = a ^ B = a. Similarly, a = B ^ a = B; easy exchange.
The above three algorithms implement the exchange of two variable values without using other variables. In comparison, the arithmetic algorithm and bit algorithm can only exchange standard data types in pure C, there is nothing to do with the struct, while the heavy-load operation in C ++ can also complete the exchange of complex types, and the pointer algorithm can easily implement the exchange of any data structure.
The introduction of these three algorithms is not to be applied to practice, but to explore technology and demonstrate the charm of program design. It can be seen that the tips in mathematics have a considerable influence on programming, and using them properly will have unexpected magical effects. From the perspective of actual software development, standard algorithms are undoubtedly the best and can solve any type of exchange problems.