Broadcom Interview Question

How to swap the values in 2 variables without using a temporary variable.

Interview Answers

Anonymous

Mar 17, 2010

a=a^b b=b^a a=a^b

12

Anonymous

Jul 10, 2011

but it should say swap two integers! because this XOR won't work with float for example

4

Anonymous

Jul 15, 2011

This works for floats: a = a+b b = a-b a = a-b But you might overflow on a+b.

5

Anonymous

Dec 12, 2014

push and pop the values from the stack.

2

Anonymous

Jul 23, 2017

While using the XOR method, add condition to check if numbers are same. If they are same, XOR results in 0.