TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: David Nugent
from: Frank Malcolm
date: 1993-06-30 06:18:20
subject: greatest common divisor

Hi, David.

DN> PE>  {
DN> PE>    temp = a;
DN> PE>    a = b;
DN> PE>    b = temp;
DN> PE>  }

DN> FM> Couldn't you avoid using the temporary variable by replacing the above
DN> FM> with
DN> FM>      {
DN> FM>        a ^= b;
DN> FM>        b ^= a;
DN> FM>        a ^= b;
DN> FM>      }


DN>There is rarely any point, and a compelling reason NOT to
DN>do this - readability.

Yep, I agree. But what do you think of Brenton's suggestion?

DN>Any optimising compiler will use temp as a register variable in this context
DN> declare it 'register' within the inner block if you're
DN>concerned about non-optimising compilers. The xor method
DN>would usually be slower (than two MOV's and one XCHG) in
DN>that case.

I'm not even up to writing C code that works, let alone is efficient :-)

DN>This is one of those cute tricks you show off at parties,
DN>but not recommended for any real code.

Well that's about what it was ("show off at parties", that is). The
"party" in this case being my new-found knowledge of compound assignment
operators in C.

Regards, FIM.

 * SLMR 2.1a * OS/2: Upgrades are free.  Who would PAY for it anyway?

--- Maximus 2.01wb

* Origin: Sydney PC Users Group - COMPAQ BBS (3:712/505)
SEEN-BY: 54/54 99 711/401 430 807 808 809 932 934 712/218 407 414 505 506 515
SEEN-BY: 712/517 537 618 623 627 704 719 713/306 888 714/906
@PATH: 712/505 623 54/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.