Swap two variables

    Date: 07/05/06 (Algorithms)    Keywords: no keywords

    People used to use a temp variable to swap two variables, here is a trick that doesn't use an additional register:

    #define SWAP(a,b) a^=b^=a^=b

    But we have a problem, that method doesn't work with floating point variables... ok, every problem has a solution, try it:

    #define SWAP2(a,b) a=a+b;b=a-b;a=a-b

    Have a good coding!

    Source: http://community.livejournal.com/algorithms/80373.html

« simple || Algorithms reference »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home