Waste less time on Facebook — follow Brilliant.
×

Swap two number without any extra variable

Suppose x = 6, y = 2

x = x + y // (6+2=8)

y = x - y // (8-2=6)

x = x - y // (8-6=2)

Now, x = 2 & y = 6

The other way is:

x = x ^ y;

y = y ^ x;

x = x ^ y;

here, ^ is bit-wise x-or operation in Computer Science

Note by Reajul Haque
10 months, 3 weeks ago

No vote yet
1 vote

Comments

Sort by:

Top Newest

If someone know much better please share with us... Reajul Haque · 10 months, 3 weeks ago

Log in to reply

×

Problem Loading...

Note Loading...

Set Loading...