Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can I swap two values without using a temporary?
How can I swap two values without using a temporary?
✍: Guest
The standard hoary old assembly language programmer's trick is:
a ^= b;
b ^= a;
a ^= b;
But this sort of code has little place in modern, HLL programming. Temporary variables are essentially free, and the idiomatic code using three assignments, namely
int t = a;
a = b;
b = t;
is not only clearer to the human reader, it is more likely to be recognized by the compiler and turned into the most-efficient code (e.g. perhaps even using an EXCH instruction). The latter code is obviously also amenable to use with pointers and floating-point values, unlike the XOR trick.
2015-02-06, 1440👍, 0💬
Popular Posts:
Would I use print "$a dollars" or "{$a} dollars" to print out the amount of dollars in this example?...
If XML does not have closing tag will it work? No, every tag in XML which is opened should have a cl...
How Many Types of Tables Supported by Oracle? - Oracle DBA FAQ - Managing Oracle Database Tables Ora...
Can you explain in brief how the ASP.NET authentication process works? ASP.NET does not run by itsel...
How To Display a Past Time in Days, Hours and Minutes? - MySQL FAQs - Managing Tables and Running Qu...