This swapping problem was assigned to my son over spring break at his university. I have been in the industry 40+ years and was not aware of this method. Even in assembly programming we used a third register as a temp which is more efficient than XOR, and there is no edge case. When my son presented the XOR solution, the professor did not even know that a circumflex is the XOR operator let alone how to use it. I can't wait to see the professor's solution when the assignment explicitly said "without allocating any extra storage" 🤣
Gorgeous visuals, the part that helped me was realizing that properties apply to propositional operations. I assume distributive property could also work?
@@theteachr the result would be 0. But I tried running the code in a variety of languages and the variables stay the same after the executing the expressions
The point you’re (I did too) missing is that a and b are pointers. When a and b are the same, it means that they’re pointing to the same variable. The first statement will make the variable 0. b in the next statement will also be 0 as it was pointing to the same location.
This swapping problem was assigned to my son over spring break at his university. I have been in the industry 40+ years and was not aware of this method. Even in assembly programming we used a third register as a temp which is more efficient than XOR, and there is no edge case. When my son presented the XOR solution, the professor did not even know that a circumflex is the XOR operator let alone how to use it. I can't wait to see the professor's solution when the assignment explicitly said "without allocating any extra storage" 🤣
Post the prof’s solution once they do show it. Although the one below is what I’ve most commonly seen:
a = a + b;
b = a - b;
a = a - b;
@@theteachr That's exactly what she provided. She failed to mention however that it can cause an overflow or underflow condition
Very nice video, it explained things quite clearly in a way that other videos did not
omg cs 2nd grade student and this... this amazed me. extremely sophisticated work thanks bro
Gorgeous visuals, the part that helped me was realizing that properties apply to propositional operations. I assume distributive property could also work?
It doesn’t distribute over addition.
1 ^ (1 + 2) ≠ 1 ^ 1 + 1 ^ 2
Awesome video ! What is the name of the monospaced font you used ?
Thank you! It's ProFont X.
@@theteachr thank you
Darn that edge case is really bugging me, I can't seem to figure it out... Love your vids btw
Stop reading this comment if you are still trying to figure it out on your own.
What is a ^ a?
@@theteachr a ^ a = 0... but I'm a little confused how this would ruin the data, because a ^ 0 = a so you can still retrieve the number
@@ExplosiveLizard What do you think would happen if you call the routine on two different variables that had the same value?
@@theteachr the result would be 0. But I tried running the code in a variety of languages and the variables stay the same after the executing the expressions
@@theteachr so in C:
void xorswap(int a, int b) { ... }
(a != b) ? xorswap(a, b) : NULL;
Not ansi compliant btw
Very well explained.
@theteachr I'm so confused like if a and b were 9 like wouldn't this happen
a = 9^9 = 0
b = 9^0 = 9
a = 9^0 = 0
so nothing would change??
I need to &my_mistake. Your last swapping line should have been `a = 9 ^ 0 = 9`, by the way.
@@theteachr thanks I don't know how I even made such a simple mistake
The point you’re (I did too) missing is that a and b are pointers. When a and b are the same, it means that they’re pointing to the same variable. The first statement will make the variable 0. b in the next statement will also be 0 as it was pointing to the same location.
@@theteachr Ok I see now, thanks
You opened my eyes bro(*´︶`*)♡Thanks!
Well explained 🔥🔥
Thank you !!!
❤❤
Isn’t the edge case when a and b are equal?
It is.
2nd line should have been b = b ^ a?
XOR is commutative. Works fine, unless I'm wrong.
@@theteachr right, my bad