Local variables, bitwise exclusive OR, addition and subtraction of the number of switching values of the two -C / C ++ program base (VI)

table of Contents

 

Local variable temp to complete the exchange (optimum)

Used to complete the exchange (suboptimal) bitwise exclusive-OR operation

Use addition and subtraction to complete the exchange (the worst)

Summarizes the advantages and disadvantages of three ways


Local variable temp to complete the exchange (optimum)

#include<iostream>
#include <stdio.h>

void swap1(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
};

int main()
{
	int x = 5, y = 8;
	printf("x:%d, y:%d\n", x, y);
	swap1(x, y);
	printf("x:%d, y:%d\n", x, y);
	system("pause");
	return 0;
}

Complete the exchange: 

Used to complete the exchange (suboptimal) bitwise exclusive-OR operation

XOR operation rule is: For the two involved in computing the number of bits each corresponding to different or, if the two values ​​are not the same as the respective bits, the exclusive OR result is 1. If the two values ​​of the respective two bits of the same value, the exclusive OR result is 0. Thereafter, the binary bits then converted to decimal bit output.

a

b

a⊕b

1

0

1

1

1

0

0

0

0

0

1

1

#include<iostream>
#include <stdio.h>

void swap3(int& a, int& b)
{
	a ^= b;
	b ^= a;
	a ^= b;
};

int main()
{
	int x = 5, y = 8;
	printf("x:%d, y:%d\n", x, y);
	swap3(x, y);
	printf("x:%d, y:%d\n", x, y);
	system("pause");
	return 0;
}

Complete the exchange: 

It is a strange phenomenon, if the corresponding bits of the same, the result is 0, otherwise the result is 1, such that the continuous operation can be switched 3 times a, b values.

 

Use addition and subtraction to complete the exchange (the worst)

#include<iostream>
#include <stdio.h>

void swap2(int& a, int& b)
{
	a = a + b;
	b = a - b;
	a = a - b;
};

int main()
{
	int x = 5, y = 8;
	printf("x:%d, y:%d\n", x, y);
	swap2(x, y);
	printf("x:%d, y:%d\n", x, y);
	system("pause");
	return 0;
}

Complete the exchange: 

Summarizes the advantages and disadvantages of three ways

The above three methods, when to use addition and subtraction to achieve the exchange, it is possible the problem of data overflow will occur, compared to this, then use the bitwise exclusive-or set of temporary variables and the way to secure more. However, with the press when the bit-wise exclusive or, every time the data to be read into two registers, then the arithmetic operation, then write the result back to the variable, after a total of three memory write operation required. , Inefficient, and will result in very poor readability of the code, the most important thing is, if you use C language to achieve the above two methods, using the compiler and gcc compiler, you can use the command gcc -S swap.c view the assembly code, the number of lines of code less temporary variables method, additional use  gcc  compiler, an exclusive oR operation with switching array error. Therefore, the use of temporary variables embodiment, each assignment as long as the value of a variable to the reading register, and then write back from the register variable to another, two memory write operation directed to the front and rear, and safer compared and more efficient!

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104893382