Click here to Skip to main content
15,896,207 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: swap two values withou using temp. storage [modified] Pin
Nuri Ismail21-Jul-10 21:55
Nuri Ismail21-Jul-10 21:55 
GeneralRe: swap two values withou using temp. storage Pin
patbob23-Jul-10 7:04
patbob23-Jul-10 7:04 
GeneralRe: swap two values withou using temp. storage Pin
Aescleal21-Jul-10 22:10
Aescleal21-Jul-10 22:10 
GeneralRe: swap two values withou using temp. storage Pin
Nuri Ismail21-Jul-10 22:19
Nuri Ismail21-Jul-10 22:19 
GeneralRe: swap two values withou using temp. storage Pin
Emilio Garavaglia22-Jul-10 7:03
Emilio Garavaglia22-Jul-10 7:03 
GeneralRe: swap two values withou using temp. storage Pin
Aescleal22-Jul-10 20:04
Aescleal22-Jul-10 20:04 
GeneralRe: swap two values withou using temp. storage Pin
Vikram A Punathambekar23-Jul-10 7:21
Vikram A Punathambekar23-Jul-10 7:21 
AnswerRe: swap two values withou using temp. storage Pin
Aescleal21-Jul-10 22:09
Aescleal21-Jul-10 22:09 
If you're using C the various tricks other people have shown you will work, well, the XOR trick will, the add'em subtract'em one won't in all cases (you can't add two pointers although you can subtract them but can't use -=).

Having said that I wouldn't bother too much unless it's an accademic exercise. Years ago when I worked in the games industry there were loads of tricks like this one which were meant to save clock cycles. However when, after loads of micro-optimisations like this one, someone actually did any timings there didn't turn out to be a lot of difference.

Cheers,

Ash
GeneralRe: swap two values withou using temp. storage Pin
Peter Mulholland23-Jul-10 1:38
Peter Mulholland23-Jul-10 1:38 
GeneralRe: swap two values withou using temp. storage Pin
Aescleal23-Jul-10 4:07
Aescleal23-Jul-10 4:07 
AnswerRe: swap two values withou using temp. storage Pin
Maximilien22-Jul-10 2:53
Maximilien22-Jul-10 2:53 
GeneralRe: swap two values withou using temp. storage Pin
Emilio Garavaglia22-Jul-10 7:55
Emilio Garavaglia22-Jul-10 7:55 
QuestionRe: swap two values withou using temp. storage Pin
Niklas L22-Jul-10 10:18
Niklas L22-Jul-10 10:18 
AnswerRe: swap two values withou using temp. storage Pin
Emilio Garavaglia22-Jul-10 11:29
Emilio Garavaglia22-Jul-10 11:29 
AnswerRe: swap two values withou using temp. storage Pin
waldemar.sauer@aitmetis.com23-Jul-10 7:58
waldemar.sauer@aitmetis.com23-Jul-10 7:58 
GeneralRe: swap two values withou using temp. storage Pin
Niklas L23-Jul-10 20:42
Niklas L23-Jul-10 20:42 
GeneralRe: swap two values withou using temp. storage Pin
stevev623-Jul-10 2:37
stevev623-Jul-10 2:37 
AnswerRe: swap two values withou using temp. storage Pin
Craig Norton23-Jul-10 3:31
Craig Norton23-Jul-10 3:31 
AnswerRe: FAST! swap two values without using temp. storage PinPopular
ghle23-Jul-10 4:20
ghle23-Jul-10 4:20 
AnswerRe: swap two values withou using temp. storage Pin
BC3Tech23-Jul-10 4:33
BC3Tech23-Jul-10 4:33 
AnswerRe: swap two values withou using temp. storage Pin
EbenRoux23-Jul-10 4:48
EbenRoux23-Jul-10 4:48 
AnswerRe: swap two values withou using temp. storage Pin
codemunkeh23-Jul-10 5:20
codemunkeh23-Jul-10 5:20 
AnswerRe: swap two values withou using temp. storage Pin
Igor Jerosimic23-Jul-10 5:36
Igor Jerosimic23-Jul-10 5:36 
JokeRe: swap two values withou using temp. storage Pin
Member 322138523-Jul-10 7:16
Member 322138523-Jul-10 7:16 
AnswerRe: swap two values withou using temp. storage Pin
Kenneth Kasajian23-Jul-10 14:40
Kenneth Kasajian23-Jul-10 14:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.