Original Post
I came across this trick for swapping...any comments? x = x+y -(y=x) ;
Quote:
Original post by rpg_code_master
x ^= y ^= x ^= y;
I actually got asked that question at an interview once. It's so usful to know
void swap(int &x, int &y){ x = x+y -(y=x);}int main(int, char **){ int a = 42; int b = 10; cout << "a = " << a << ", " << "b = " << b << endl; swap(a, b); cout << "a = " << a << ", " << "b = " << b << endl;}a = 42, b = 10a = 42, b = 42a = 42, b = 10a = 10, b = 42Quote:
Original post by Lerner Quote:
Original post by rpg_code_master
x ^= y ^= x ^= y;
I actually got asked that question at an interview once. It's so usful to know
Above code has undefined behaviour. Can anyone tell me about my original code. Is it also undefined?
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more