| | |
Swapping Without Third Variable
![]() |
http://www.catb.org/~esr/faqs/smart-...html#writewell
Yes you can http://c-faq.com/expr/xorswapexpr.html
But that is a dumb trick which ceased to be of any importance when we came down from the trees and started using high level programming languages.
Yes you can http://c-faq.com/expr/xorswapexpr.html
But that is a dumb trick which ceased to be of any importance when we came down from the trees and started using high level programming languages.
Here's anothe way
c Syntax (Toggle Plain Text)
x -= y; y += x; // y becomes x x = (y - x); // x becomes y
I would really suggest to use the temp variable to swap two values. Cos the swapping variables without third variable such as using the arithmetic operators. This would really very help only when you wanted to swap unsigned integers. Apart from that it is not a recommended at all. If anything other unsigned integers you need to use the temp variable to swap. But in over all the latter would be efficient way of doing it.
ssharish
ssharish
![]() |
Similar Threads
- Memory access violations (C)
- swapping (C)
- muliplying without using a * operator! (C++)
- Need Help with variable initialization (Java)
- variable function parameters functionname(int a, ...) (C)
- Retreiving a Variable from a URL (PHP)
- Can draw one Frame in a variable? (Java)
- Pipe inside variable isn't working in bash (Shell Scripting)
Other Threads in the C Forum
- Previous Thread: New Experiment Messin With
- Next Thread: Huffman Coding
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






