V
viv342
how can two variables be swapped without using a third variable?
viv342 said:how can two variables be swapped without using a third variable?
how can two variables be swapped without using a third variable?
viv342 said:how can two variables be swapped without using a third variable?
a = a^b;
b = a^b;
a = a^b;
a = a^b;
b = a^b;
a = a^b;
Quess who missed his Easter bonus at work.Martin Ambuhl said:You didn't check the FAQ before posting this trite question, did you? Nor
did you check the archives, did you?
When you go back to the idiot that thinks the stupid XOR trick is neat,
ask him what he does with non-integer variables. And ask him how he knows
the compiler doesn't generate a hidden third variable even when he uses
the stupid XOR trick with integer type variables. And if he gives you a
one-line answer, it is sure to violate constraints even on integer
variables. Learn some real programming and leave the crap to people who
place bar bets for drinks.
Quess who missed his Easter bonus at work.
The XOR trick was new to me once.
viv342 said:how can two variables be swapped without using a third variable?
how can two variables be swapped without using a third variable?
So you know the stupid XOR trick. So what? Don't perpetuate this.
Just because every class of beginners has someone who thinks this is
cool doesn't mean that it is worth bothering with. And you neither
answered the question about what happens with non-integer variables nor
demonstrated why you think the compiler doesn't generate a hidden third
variable. If your trick doesn't work with non-integers (it doesn't),
then the original question has not been answered. If the compiler might
generate a hidden third variable, then the original question has not
been answered.
#define MAXSWAP 32 /* maximum size of variable swap */
int swap(void *a, void *b, int size)
{
char temp[MAXSWAP]; /* temp buffer */
if (size > MAXSWAP) return(-1);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return(0);
}
Now what's wrong with that?
Daniel said:To the OP:
#define MAXSWAP 32 /* maximum size of variable swap */
int swap(void *a, void *b, int size)
{
char temp[MAXSWAP]; /* temp buffer */
if (size > MAXSWAP) return(-1);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return(0);
}
Now what's wrong with that?
You meant a = a+b;Vishesh said:a = a+b
Ian Collins said:Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
Army1987 said:Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
How 'bout:
int swap (void *a, void *b)
{
void *temp;
if (sizeof *a != sizeof *b) {
retun 0;
Vishesh said:a = a+b
b = a-b;
a = a-b;
Malcolm McLean said:Quess who missed his Easter bonus at work.
TINEB.
The XOR trick was new to me once.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.