Interview Question

Interview

swap two numbers without using temporary variable

Answer

Interview Answer

2 Answers

0

b=a+b; a=b-a; b=b-a;

sagar on May 15, 2011
0

Sagar's answer is decent, but there might be problems if they consider overflow. Better is using xor: b = a XOR b a = a XOR b b = a XOR b

David on May 30, 2011

Add Answers or Comments

To comment on this, Sign In or Sign Up.