Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

I think "XOR Swap" sums up my opinion on most Tech Interview Questions.

The only question I would ever ask about the XOR Swap trick would probably be "Give 3 reasons never to use this" (assuming you are in a language as least as high level as C).



Hm, I thought the reason for not using it is hardware-related (if your CPU is multi-issue, the dependencies between the three assignments kill performance). What are the language-specific reasons?


Reasons not to use XOR swap:

1) It obfuscates (the most obvious thing, but still worth mentioning).

1) It is incorrect if you swap a memory location with itself (which many algorithms, like sorts, can end up doing without meaning to).

2) It is always slower on any modern optimising compiler than using a temporary in Java, C and C++ (a strong claim to make, but I stand by it. That's why I list specific languages where I know about the internals). Modern compilers use "flow" techniques for temporary variables. A swap will usually compile away to the compiler internally relabelling which value is in which register, and therefore no code at all.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: