+ 1

swap two number without the help of other variable

only two variable is used in the program

5th Jul 2016, 7:19 AM
DEATH WALKER
DEATH WALKER - avatar
7 Respostas
+ 4
@Dorian correct
5th Jul 2016, 7:55 AM
DEATH WALKER
DEATH WALKER - avatar
+ 3
#include <iostream> using namespace std; int main() { int a=42, b=666; a=a+b; b=a-b; a=a-b; return 0; }
5th Jul 2016, 7:32 AM
Dorian
+ 2
int a = 42; int b = 13; a ^= b; b ^= a; a ^= b; Or in some compilers: a ^= b ^= a ^= b; (Which is very beautiful) Now a = 13 and b = 42.
5th Jul 2016, 12:23 PM
Garme Kain
Garme Kain - avatar
+ 2
great solution, with the xor!!!
5th Jul 2016, 2:44 PM
Dorian
0
what is ^= ?
5th Jul 2016, 12:52 PM
Dorian
0
a ^= b -> a = a ^ b where ^ is the bitwise xor operator.
5th Jul 2016, 1:12 PM
Garme Kain
Garme Kain - avatar
0
a,b=b,a.
9th Jun 2017, 10:01 AM
R.kishore
R.kishore - avatar