+ 1
swap two number without the help of other variable
only two variable is used in the program
7 ответов
+ 4
@Dorian
correct
+ 3
#include <iostream>
using namespace std;
int main() {
int a=42, b=666;
a=a+b;
b=a-b;
a=a-b;
return 0;
}
+ 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.
+ 2
great solution, with the xor!!!
0
what is ^= ?
0
a ^= b -> a = a ^ b
where ^ is the bitwise xor operator.
0
a,b=b,a.