Pregunta de entrevista de BlackBerry

Switch two variables, x and y, without using the third.

Respuesta de la entrevista

Anónimo

9 jun 2011

Let us say x = 5, y = 10 x = x + y // x = 15, y = 10 y = x - y // y = 5, x = 15 x = x - y // x = 10, y =5 You can also use a bitwise operator, XOR, in case the numbers are huge (overflow when adding together) x = x^y y = y^x x = x^y