Commutativity only matters depending on how you write the statements. What really matters is associativity: that you can rewrite (A ^ B) ^ B as A ^ B ^ B and then A ^ (B ^ B) which gets you A ^ 0, which is A. XOR swap is nice because you can write it as 'A ^= B ^= A ^= B', which is one printable character and two spaces shorter than 'T = A, A = B, B = T'.
It's visual studio. Free IDE from MS, search for "Visual Studio 2015 Community edition" and you should find it. They've recently added python to it, but you might have to install it as an extension. Good luck and thanks for watching!
Thank you for the video! very informative. But I have one question, is there a reason why using the same value did not result in zero when using non-pointer values? I tried using a = b, but the result was just fine.
In that case, A^B would be zero, but since they're both the same value, you still have the value saved. When you XOR again, you get it back. If you're using pointers, you overwrite the only value you have, then when you XOR again, you're just XORing two 0's. Hope that makes sense, have a good one and happy new year!
It works with floats and any other data. But C++ is typesafe and it's not good at using the bits of a float as an int. If you're using Assembly, it's easy since Assembly doesn't care about data types. But in C++ there's too much wrangling involved with treating the bits as an int without casting them. Have a look a the code below, it performs the XOR swap between two floats: float a = -12.8f, b = 4.3f; int* A, *B; // tmp vars for int versions // Point to bits of a and b without casting A = (int*)&a; B = (int*)&b; // XOR swap *A = *A ^ *B; *B = *A ^ *B; *A = *A ^ *B; Cheers, have a good one!
Commutativity only matters depending on how you write the statements.
What really matters is associativity: that you can rewrite (A ^ B) ^ B as A ^ B ^ B and then A ^ (B ^ B) which gets you A ^ 0, which is A.
XOR swap is nice because you can write it as 'A ^= B ^= A ^= B', which is one printable character and two spaces shorter than 'T = A, A = B, B = T'.
There is an even better version of this that works with all numerical types:
A = A + B
B = A - B
A = A - B
Hey,
how did you show line by line execution of program?
what kind of IDE are you using?
I want to see line by line execution of large python scripts.
It's visual studio. Free IDE from MS, search for "Visual Studio 2015 Community edition" and you should find it. They've recently added python to it, but you might have to install it as an extension. Good luck and thanks for watching!
Thank you for the video! very informative. But I have one question, is there a reason why using the same value did not result in zero when using non-pointer values? I tried using a = b, but the result was just fine.
In that case, A^B would be zero, but since they're both the same value, you still have the value saved. When you XOR again, you get it back. If you're using pointers, you overwrite the only value you have, then when you XOR again, you're just XORing two 0's. Hope that makes sense, have a good one and happy new year!
Thank you so much for the reply! definitely makes sense.
Hope for you a happy new year too!
Does this work without integers?
It works with floats and any other data. But C++ is typesafe and it's not good at using the bits of a float as an int. If you're using Assembly, it's easy since Assembly doesn't care about data types. But in C++ there's too much wrangling involved with treating the bits as an int without casting them.
Have a look a the code below, it performs the XOR swap between two floats:
float a = -12.8f, b = 4.3f;
int* A, *B; // tmp vars for int versions
// Point to bits of a and b without casting
A = (int*)&a;
B = (int*)&b;
// XOR swap
*A = *A ^ *B;
*B = *A ^ *B;
*A = *A ^ *B;
Cheers, have a good one!
Thanks!! Great response.
7:47
not all heroes wear capes!