Too much discussion of the XOR swap trick

https://heather.cafe/posts/too_much_xor_swap_trick/

Article

  • Post examines the XOR swap trick: swapping two vars without a temp variable
  • Compiler spotted and optimized the pattern, making benchmarking moot
  • Broader look at XOR’s mathematical properties and practical uses

Discussion

  • XOR’s real value: erasure coding (a^b^c parity for packet recovery)
  • Undefined-behavior form a^=b^=a^=b called out as dangerous competitive-programming relic
  • SSE/SIMD conditional swap via XOR+mask cited as a genuinely useful application
  • Consensus: trick is obsolete; compilers handle it better

Discuss on HN


Type Link
Added Apr 16, 2026
Modified Apr 16, 2026