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 ~~