Click here to Skip to main content
15,891,431 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
Use the processor registers. No temp variable involved. Use assembly language of your choice.

Fast! 4 CPU instructions w/4 memory waits & you're done!

int x = 150;
int y = 45;
asm{
   LDA x;
   LDB y;
   STA y;
   STB x;
}


Using XOR, do it 3 times slower...
11 CPU instructions w/7 memory waits
int x = 150;
int y = 45;
x ^= y;
y ^= x;
x ^= y;

#DEFINE B 1
int x = 150;
int y = 45;
asm{
   /* x ^= y */
   LDA x;  /* A register = x */
   XOR y;  /* A = A XOR y */
   STA x;  /* x = A, i.e., x ^= y */

   /* y ^= x */
   STA B;  /* B register = x */
   LDA y;  /* A register = y */
   XOR B;  /* A = A XOR B */
   STA y;  /* y = A, i.e., y ^= x  */

   /* x ^= y */
   STA B;  /* B register = y */
   LDA x;  /* A register = x */
   XOR B;  /* A = A XOR B */
   STA x;  /* x = A, i.e., x ^= y  */
}




Otherwise, this other SLOW method...
12 CPU instructions w/6 memory waits
x += y;
y = x - y;
x -= y;

#DEFINE B 1
int x = 150;
int y = 45;
asm{
   /* x = x + y; */
   LDA x;  /* A register = x */
   LDB y;  /* B register = y */
   ADA B;  /* A register = A + B, i.e. A = x + y */
   STA x;  /* Save A register to x */

   /* y = x - y */
   /* A contains x, B contains y */
   CMB;   /* 1's complement B register, i.e. B = -y -1 */
   INB;   /* increment (2's complement) B , i.e. B = -y */
   ADA B; /* Add B (-y) to A (x), i.e. A = x - y */
   STA y; /* Save A register to y */

   /* x = x - y */
   /* At this point, A contains y */
   CMA;   /* 1's complement A register, i.e. A = -y -1 */
   INA;   /* increment A (2's complement), i.e. A = -y */
   ADA x; /* Add x to A, i.e., A = -y + x */
   STA x; /* Save A register to x */
}


Notes for the unknowing:
Operations on registers are much faster than memory access
Negating a number is two's complement
Two's complement is one's complement +1
One's complement reverses (NOTs) all bits


Who loves Assembler??? Thumbs Up | :thumbsup:
No JITCHIT Dead | X| either.
Gary

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 
AnswerRe: swap two values withou using temp. storage Pin
manjulameganathan23-Jul-10 18:00
manjulameganathan23-Jul-10 18:00 
AnswerRe: swap two values withou using temp. storage Pin
Jigar Sheth25-Jul-10 20:38
Jigar Sheth25-Jul-10 20:38 
QuestionWaveInOpen Help?? Pin
AmbiguousName21-Jul-10 19:36
AmbiguousName21-Jul-10 19:36 
AnswerRe: WaveInOpen Help?? Pin
«_Superman_»21-Jul-10 19:38
professional«_Superman_»21-Jul-10 19:38 
QuestionHow can disable a subitem where i use COutlook2Ctrl::OCL_SELECT? Pin
Le@rner21-Jul-10 19:23
Le@rner21-Jul-10 19:23 
Questionhow Does CWinApp realize the singleton pattern? Pin
zxjun8421-Jul-10 18:54
zxjun8421-Jul-10 18:54 
AnswerRe: how Does CWinApp realize the singleton pattern? Pin
«_Superman_»21-Jul-10 19:45
professional«_Superman_»21-Jul-10 19:45 
GeneralRe: how Does CWinApp realize the singleton pattern? Pin
zxjun8421-Jul-10 20:39
zxjun8421-Jul-10 20:39 
GeneralRe: how Does CWinApp realize the singleton pattern? Pin
«_Superman_»21-Jul-10 21:26
professional«_Superman_»21-Jul-10 21:26 
GeneralRe: how Does CWinApp realize the singleton pattern? Pin
zxjun8421-Jul-10 22:18
zxjun8421-Jul-10 22:18 
Questionproblem between OS of 32bit ans 64 bits Pin
xjh_sz21-Jul-10 15:03
xjh_sz21-Jul-10 15:03 

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.