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

It's not quite the same as XOR swap, but a trick I've found handy is conditionally swapping values using XOR:

    int a, b;
    bool cond;
    
    int swap = cond ? a ^ b : 0;
    a ^= swap;
    b ^= swap;
If cond is highly unpredictable this can work rather nicely.


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

Search: