3 ms·
std::swap<>? CMPXCHG? LDREX/STREX? The XOR trick is cute but you shouldn't use it on anything larger or more modern than a Cortex-M0. Just put it through a tem
by pjc50 7y ago
std::swap<>? CMPXCHG? LDREX/STREX?
The XOR trick is cute but you shouldn't use it on anything larger or more modern than a Cortex-M0. Just put it through a temporary variable and let register renaming deal with it.
(Perhaps a more interesting question would be a multi-threaded examination of swapping two numbers...)
- verall 7y agoIt's interesting because this is a real answer that will get you hired. It demonstrates knowledge of: the "correct" way to do it, basic asm awareness, programmer culture, and computer architecture. This probably checks off like 6 boxes for most technical interviewers in one answer. Finally it implies it could have asked a better interview question ;)
- pjc50 7y agoThankyou. I try to strike a balance with these things - showing off is mandatory in interviews, but needs to be done politely. You don't want to go full "Hexing the Technical Interview": https://aphyr.com/posts/341-hexing-the-technical-interview https://aphyr.com/posts/341-hexing-the-technical-interview
- taneq 7y agoThis is basically the perfect answer to that question.