Deutsche Bank Interview Question

Is it possible to swap two registers without using a third one?

Interview Answers

Anonymous

Nov 20, 2009

Add two variables and assign the value into First variable. Subtract the Second value with the result Value. and assign to Second variable. Subtract the Result of First Variable With Result of Second Variable and Assign to First Variable. Example: int a=5,b=10;a=a+b; b=a-b; a=a-b;

1

Anonymous

Nov 14, 2010

When we talk of registers XORing is the operation that we can perform. Say a = 5 ; b = 10 a = a XOR b; // 5 XOR 10 is 15 so "a = 15" b = a XOR b // 15 XOR 10 is 5 so "b = 5" a = a XOR b // 15 XOR 5 is 10 so "a = 10" So in the end a = 10 and b = 5

Anonymous

Oct 6, 2012

Using XOR is probably better than using addition as addition might cause overflow.