I've written my answer with C in mind, and it does explain how it works if you try the calculation by hand: onlinegdb.com/B1IY6hFK7
#include <stdio.h> int main() { int a = 77; int b = 42; printf("a: %d, b: %d \n", a, b); swap(&a, &b); printf("after swap \n"); printf("a: %d, b: %d", a, b); return 0; } void swap(int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }
When both a and b has the same value: onlinegdb.com/r1WeypFF7
a
b
Result may vary depending on the language compiler, you might have to adjust the snippet according to your language's nuances.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I've written my answer with C in mind, and it does explain how it works if you try the calculation by hand: onlinegdb.com/B1IY6hFK7
When both
a
andb
has the same value: onlinegdb.com/r1WeypFF7Result may vary depending on the language compiler, you might have to adjust the snippet according to your language's nuances.