bill Posted June 18, 2006 Report Share Posted June 18, 2006 Hello ;DStill learning, i wonder :Is there an elegant coding way of swapping bytes in a array ?ex : swapping arp[1] with arp[2] so arp(0,7,3) become arp(0,3,7),without using another byte ??? Quote Link to comment Share on other sites More sharing options...
fluke Posted June 18, 2006 Report Share Posted June 18, 2006 There's always this classic algorithm for swapping variables without a temporary variable: x = x ^ y; y = x ^ y; x = x ^ y; ( ^ is the xor operator)It works as long as x and y are not the same variable (ie, not the same address) Quote Link to comment Share on other sites More sharing options...
OrganGrinder Posted June 18, 2006 Report Share Posted June 18, 2006 hi billi suppose that flukes solution might work, but it would be harder for you to read (and understand) later on.i would stay with the temporary variable, but you would limit the context by enclosing within braces {}int x,y;...{ int tmp; tmp = x; x = 7; y = tmp;}this way the compiler has the option of what to do with the tmp variable, it may only use it in that part of code, or it may initialise it at the beginning of the function. as i said, its the compilers choice. Quote Link to comment Share on other sites More sharing options...
Thomas Posted June 18, 2006 Report Share Posted June 18, 2006 I guess that makes no difference. Look at the resulting asm-file! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.