In C
1 2 3 4 | void swap(int &x, int &y) { x ^= y ^= x ^= y; } |
In Java
1 2 3 4 | public void swap(int [] a) { assert a.length == 2; a[0] ^= a[1] ^= a[0] ^= a[1]; } |
In Assembler
1 2 3 4 5 6 | __asm { mov eax,a mov ebx,b mov b,eax mov a,ebx } |
Fun fact: fastest is still with temp variable. (:
1 2 3 4 5 6 | public void swap(int &a, int &b) { int c = a; a = b; b = c; } |
Extra python:
1 | a, b = b, a |
Leave a Reply