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

C / C++ / MFC

 
AnswerRe: Wrapper Class Pin
Sauro Viti21-Jul-10 22:44
professionalSauro Viti21-Jul-10 22:44 
AnswerRe: Wrapper Class Pin
Niklas L21-Jul-10 23:01
Niklas L21-Jul-10 23:01 
AnswerRe: Wrapper Class Pin
«_Superman_»21-Jul-10 23:12
professional«_Superman_»21-Jul-10 23:12 
AnswerRe: Wrapper Class Pin
Aescleal21-Jul-10 23:13
Aescleal21-Jul-10 23:13 
Questionswap two values withou using temp. storage Pin
lgmanuel21-Jul-10 20:10
lgmanuel21-Jul-10 20:10 
AnswerRe: swap two values withou using temp. storage PinPopular
«_Superman_»21-Jul-10 20:25
professional«_Superman_»21-Jul-10 20:25 
AnswerRe: swap two values withou using temp. storage Pin
Luc Pattyn21-Jul-10 20:26
sitebuilderLuc Pattyn21-Jul-10 20:26 
AnswerRe: swap two values withou using temp. storage [modified] PinPopular
Nuri Ismail21-Jul-10 20:29
Nuri Ismail21-Jul-10 20:29 
1)
With simple arithmetic operations:
int x = 150;
int y = 45;
x = x + y;
y = x - y;
x = x - y;


[EDIT]
The above technique is dangerous because in some situations you can exceed the data types range!!

Now I know (thanks to emilio_grv and his excellent post[^]) that this technique is not dangerous if the two variables are of the same integer type.
[/EDIT]

2)
The XOR swap:
int x = 150;
int y = 45;
x ^= y;
y ^= x;
x ^= y;


You can read more about the XOR swap here[^].

Also you can check out some CodeProject articles (and their message boards) on this topic:
- A Curious Economic Swap[^]
- The Stupid XOR Trick[^]

Smile | :)

modified on Thursday, July 22, 2010 3:56 AM

GeneralRe: swap two values withou using temp. storage PinPopular
Emilio Garavaglia21-Jul-10 21:41
Emilio Garavaglia21-Jul-10 21:41 
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 
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 

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.