[C/C ++] an exclusive or operation cleverly achieves an exchange operation between two numbers

Source: Internet
Author: User

Today, we are looking at how OpenGL loads TGA images as textures.CodeWhen I saw a line of code about RGB (a) sequence conversion, I was confused at first. Later I thought about how to implement the switching operation.

Original code:

 
Texture-> imagedata [cswap] ^ = texture-> imagedata [cswap + 2] ^ = texture-> imagedata [cswap] ^ = texture-> imagedata [cswap + 2];

I wrote a piece of code to test it:

 
# Include <iostream> using namespace STD; int main () {int A = 1; int B = 2; a ^ = B ^ = a ^ = B; cout <"A =" <A <Endl; cout <"B =" <B <Endl ;}

Running result:

^ In C, the bitwise XOR operator is used. If the bitwise XOR operator is used, the result is 0. If it is different, the result is 1.

In fact, you can use a pen to push it.

First, the operation order is from right to left.

Assume that the original values of A and B are recorded as A0 and B0.

After the rightmost ^ = operation:

B remains unchanged. B = b0.0.

A = A0 ^ B0;

The second to the last ^ = after the operation:

A remains unchanged. A = A0 ^ B0;

B = b0 ^ A = b0 ^ (A0 ^ B0) = b0 ^ (B0 ^ A0) = A0;

After the first ^ = operation:

B remains unchanged. B = A0;

A = a ^ B = (A0 ^ B0) ^ a0 = B0.

In this way, the exchange between numbers A and B is realized.

To be honest, the first time I saw such a statement, it was really confusing. It is true that such an operation saves space compared to declaring a temporary variable. However, the Code is not very readable.

I can see that there is a comment on the statement of the original code, which is XX optimized.

But I wroteProgramAfter testing, it seems that the difference or operation method is slower.

# Include <iostream> # include <time. h> # include <windows. h> using namespace STD; int main () {int A = 1; int B = 2; int temp = 0; DWORD time1 = gettickcount (); cout <"time1 =" <time1 <Endl; For (INT I = 0; I <100000000; ++ I) {// a ^ = B ^ = a ^ = B; temp = A; A = B; B = temp;} DWORD time2 = gettickcount (); cout <"time2 =" <time2 <Endl; cout <time2-time1 <Endl ;}

Common method:

Exclusive or method:

I personally think it is better to use a common method. Suggestions for passing ~~

Related Article

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.