Question d’entretien chez BlackBerry

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

Réponse à la question d'entretien

Utilisateur anonyme

9 juin 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