Created
April 6, 2012 15:25
-
-
Save lastland/2320761 to your computer and use it in GitHub Desktop.
swap two float variables via xor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
void swap(int & a, int & b) | |
{ | |
a = a ^ b; | |
b = a ^ b; | |
a = a ^ b; | |
} | |
int main() | |
{ | |
float a = 1.1, b = 2.3; | |
swap(*(int*)&a, *(int*)&b); | |
printf("%f\t%f\n", a, b); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment