Archive for September, 2007

swap without temp var

有问起这个的, 没有多神奇, 汇编大概都有这么写的, 而且有些机器也有内建的exch指令, 用的就是这个:

x = x xor y;
y = x xor y;
x = x xor y;

证明只须把三个式子连起来...于是就有如下这个: x^=y^=x^=y;

用指针的时候会有问题, 如果&x = &y, 那么第一行就把x清零了...

在依赖pipeline的cpu上速度会比常规慢...每一行都得等前一行执行完

Tags:

  • Comments (2)

    The word \F\u\c\k

    Tags:

  • Comments

    Creative Commons License  Listed on BlogShares
    This work is licensed under a
    Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.
    Powered by Wordpress 2.5. Themes based on PhoenixBlue.