3 ms·
You're wrong. Remember that XOR works on individual bits. If what you're saying was true, then swapping bits that are both set to 1 would also fail, which means
by Thorham 6y ago
You're wrong. Remember that XOR works on individual bits. If what you're saying was true, then swapping bits that are both set to 1 would also fail, which means this algorithm wouldn't work at all.
Edit: Ignore this post, I misread the original post as saying swapping the same values would fail.
- alowe 6y agoThey're not saying swapping equal valued variables breaks it. It's when the pointer is the same, using the trick to swap a variable with itself will set the variable to 0.
- Thorham 6y agoYeah, I miss-read that. Thanks for pointing that out!
- deleted 6y ago[deleted]
- dahart 6y ago> swapping bits that are both set to 1 would also fail No, swapping two 1 bits works fine. Work it out more slowly, the article covered this and why it always works. (1,1) => (1^1,1)=(0,1) => (0,1^0)=(0,1) => (0^1,1)=(1,1)