Exchange two arbitrary types of variables using an exclusive or operation

Source: Internet
Author: User

In this article, we will use the C language to implement the function of exchanging two variables of any type. It is difficult to use C for any type. If it is C ++, we can easily use the template function:

template<class T> inline void      swap(T& t1, T& t2) {     T tmp;     tmp = t1;     t1 = t2;     t2 = tmp; }

First, use ^ to exchange two integers. The code is simple but not easy to understand.

a ^= b;b ^= a;a ^= b;

Some people say that this writing method is amazing, but what I want to say is that the exclusive or operation is a very common operation in computers. understanding this algorithm is the basis for mastering the difference or. you can use the following methods to understand ^ two integers:

Consider a and B as two Boolean types first, then A and B will have four combinations

0, 0 (1) a ^ = B; to 0, 0 (2) B ^ = A; to 0, 0 (3) a ^ = B; to 0, 0

1, 0 (1) a ^ = B; to 1, 0 (2) B ^ = A; to 1, 1 (3) a ^ = B; to 0, 1

0, 1 (1) a ^ = B; to 1, 1 (2) B ^ = A; to 1, 0 (3) a ^ = B; to 1, 0

1, 1 (1) a ^ = B; to 0, 1 (2) B ^ = A; to 0, 1 (3) a ^ = B; to 1, 1

After the three sentences are executed, the values in the four combinations are exchanged.

If the bitwise operation is unrelated to the adjacent values of BITs, the char type of the eight bits, the short of the 16 bits, and long can both be exchanged using ^.

Some people think that this exclusive or operation can only be used for integer type exchange. In fact, exclusive or operation is for binary. Since all data types in the computer are saved in binary format, of course, this operation can be used.Exchange any data type with an exclusive or operation.

My solution is as follows:

 1 #define XYZ_SWAP(i, j) \
if (&i != &j) 2 { 3 switch(sizeof(i)) 4 { 5 case 1: 6 *(char*)&i ^= *(char*)&j; 7 *(char*)&j ^= *(char*)&i; 8 *(char*)&i ^= *(char*)&j; 9 break;10 case 2:11 *(short*)&i ^= *(short*)&j;12 *(short*)&j ^= *(short*)&i;13 *(short*)&i ^= *(short*)&j;14 break;15 case 4:16 *(long*)&i ^= *(long*)&j;17 *(long*)&j ^= *(long*)&i;18 *(long*)&i ^= *(long*)&j;19 break;20 case 8:21 *(long long*)&i ^= *(long long*)&j;22 *(long long*)&j ^= *(long long*)&i;23 *(long long*)&i ^= *(long long*)&j;24 break;25 default:26 for (int k = 0; k < sizeof(i); k++)27 {28 *((char*)&i + k) ^= *((char*)&j + k);29 *((char*)&j + k) ^= *((char*)&i + k);30 *((char*)&i + k) ^= *((char*)&j + k);31 }32 break;33 }34 }35 36 void main()37 {38 char ca = 10;39 char cb = 20;40 XYZ_SWAP(ca, cb);41 42 short sa = 10;43 short sb = 20;44 XYZ_SWAP(sa, sb);45 46 int ia = 10;47 int ib = 20;48 XYZ_SWAP(ia, ib);49 50 long long lla = 10;51 long long llb = 20;52 XYZ_SWAP(lla, llb);53 54 float fa = 10.01f;55 float fb = 2000.89f;56 XYZ_SWAP(fa, fb);57 58 double da = 10.01;59 double db = 2000.89;60 XYZ_SWAP(da, db);61 62 void* pa = &da;63 void* pb = &db;64 XYZ_SWAP(pa, pb);65 }

Here we use a macro definition to exchange two variables of different types. We also assume that long occupies 4 bytes.

 

Exchange two arbitrary types of variables using an exclusive or operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.