Code Optimization

Interesting things about software development and code optimization

Swap two variables without using a third one

Hello friends,


Going to ask you - do you know how to swap two variables without of using another one variable?

If your answer is no then here you are:

- we need two variables

- we need three XOR operations

int a = 1;

int b = 2;

a ^= b;

b ^= a;

a ^= b;

now b = 1 and a = 2


Thank you ;)


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Loading