Pregunta de entrevista de Microsoft

Write a function that receives two pointers, and swaps the values between them.

Respuesta de la entrevista

Anónimo

25 nov 2014

This also looked like a simple question, and was from the same interviewer as the previous one. This one is tricky, but I was already familiar with a bitwise solution to swap variables using XOR. This is exactly what he expected. He also asked me whether something could be improved, so I added the address checking after some thought. void swap (int *a, int *b) { // If addresses are equal and XOR swap is done, // both *a and *b get zeroed out as it is the same value if(a != b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } }