Hacker News new | ask | show | jobs
by jcalvinowens 61 days ago
The "party trick" is more general than just xor, for example:

    void swap(unsigned* a, unsigned* b)
    {
        *a = *a + *b;
        *b = *a - *b;
        *a = *a - *b;
    }
GCC still sees through it with restrict: https://godbolt.org/z/8n3bGha3e
3 comments

Yea this is operating on the pointer to the values, but if you use just an `unsigned a` it uses xor

clang: https://godbolt.org/z/5PEMTrxba

gcc: https://godbolt.org/z/7j8h4zo4v

This is covered in TFA.
This has overflow issues unlike xor.
Depending on what you think the "issue" is, one of:

* wrapping is well-defined behavior for unsigned integers; signed integer wrapping is UB, but is not used here.

* the equations (that a & b cancel each other out, resulting in the swap) hold even when done in a mod N ring.