brain teasers

S

santosh

viv342 said:
how can two variables be swapped without using a third variable?

Lookup what the bitwise XOR operator does. But the compiler is likely
to use a temporary anyway, and it's nearly always more readable also.
 
B

bytebro

how can two variables be swapped without using a third variable?

Do your own homework, kid.

Hint: It took no more than ten seconds to find it from the Wikipedia
home page.
 
M

Martin Ambuhl

viv342 said:
how can two variables be swapped without using a third variable?

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

santosh

a = a^b;
b = a^b;
a = a^b;

Please quote the relevant portions of the message to which you're
replying, since on Usenet itself, as opposed to Google Groups,
previous articles may not always be available or easily accessible.
It's also good practise to make a post contextually self-contained.
 
M

Martin Ambuhl

a = a^b;
b = a^b;
a = a^b;

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

Malcolm McLean

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

Al Balmer

Quess who missed his Easter bonus at work.
The XOR trick was new to me once.

Me too. In fact, I invented it, and thought I was quite clever until I
found out how many other people had invented it <g>.

My application was different, though - implementing a byte-addressing
method for the assembler of a machine which only addressed 16 bits. A
temporary would have been undesirable, because we had to support
re-entrant code.
 
R

Richard Heathfield

viv342 said:
how can two variables be swapped without using a third variable?

Pointlessly.

Temps are cheap, and the resulting code will be quicker and easier to
read than a hack. And, in any case, the hack for which you are
stretching only works for particular kinds of variable under particular
conditions.
 
D

Daniel Rudy

At about the time of 4/4/2007 11:43 AM, Martin Ambuhl stated the following:
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.

I agree. I didn't even know what the XOR trick was before this.
Looking at that code, I can't tell what it is or what it does without
getting a pencil and some paper and working it out manually.

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? That's a universal swap function that will
work with *ANY* datatype in any configuration (as far as I know) up to
32 bytes in size (int, long, float, double, char, signed, unsigned,
struct, etc...). Hell, this will work as is on any machine no matter
what the endian is (big, little, pdp, etc...). In fact, just changing
the MAXSWAP define, you can swap data of ANY size, and it's portable.

To use it?

int a, b;
....
swap(&a, &b, sizeof(int));

--or--

double x, y;
....
swap(&x, &y, sizeof(double));


It's that simple.

(Of course I probably made a simple mistake that the regulars are going
to jump all over me about.) :)

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
R

Richard Heathfield

Daniel Rudy said:

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

Not very much. Here's a less restrictive solution:

#include <string.h>

#define MAXSWAP 32

void swap(void *va, void *vb, size_t size)
{
unsigned char *a = va;
unsigned char *b = vb;
unsigned char temp[MAXSWAP] = {0};

while(size >= MAXSWAP)
{
memcpy(temp, a, MAXSWAP);
memcpy(a, b, MAXSWAP);
memcpy(b, temp, MAXSWAP);
size -= MAXSWAP;
a += MAXSWAP;
b += MAXSWAP;
}
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return;
}

It is possible that this could be done more elegantly (eliminating the
duplicated code). Given the time of day here, I'll leave that as an
exercise.
 
I

Ian Collins

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?

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);
}
 
A

Army1987

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);
}

How 'bout:
int swap (void *a, void *b)
{
void *temp;
if (sizeof *a != sizeof *b) {
fputs("Attempt to swap variables of incompatible types", stderr);
return 1;
}
temp = malloc(sizeof *a);
if (temp == NULL) {
perror("Unable to allocate memory");
return -1;
}
memcpy(temp, a, sizeof *a);
memcpy(a, b, sizeof *a);
memcpy(b, temp, sizeof *a);
free(temp);
retun 0;
}
 
I

Ian Collins

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) {

Have you tried to compile this?

Or this?
 
R

Richard Bos

Malcolm McLean said:
Quess who missed his Easter bonus at work.
TINEB.

The XOR trick was new to me once.

It was new to all of us, once. Those of us who understand programming
quickly learnt that such schoolboy tricks are not worth looking for.

Richard
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top