Jump to content

Recommended Posts

Posted

Hello  ;D

Still 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  ???

Posted

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)

Posted

hi bill

i 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...