DEV Community

Discussion on: Terrible interview question: Swap variables without a temporary

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

You can't do it with every data type, unless you write in assembler and directly access registers. High-level language virtually all say this is undefined behaviour. You can't treat pointers, or any other type as an unsigned 2's complement integer -- the type where this xor'ing works. It's either unspecified, or even undefined behaviour.

Thread Thread
 
nikodannemiller profile image
Niko Dannemiller

That's fair. My personal experience is a bit limited so it's always good reading articles like this.