Re: brain teasers



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


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
.



Relevant Pages

  • Re: why use fprintf / size_t instead of printf/ int
    ... I could not find the original question either, ... The base address is of a pointer type, ... likely to be the the same size as a size_t, but whether any conversion ... size_t should be converted than int. ...
    (comp.lang.c)
  • Re: question on member functions
    ... wanting to access functions in an implementation file for user defined classes ... Supose myClass contains a function 'int func ... Unless I totally misunderstood the original question. ...
    (comp.lang.cpp)
  • Re: VS2005 doesnt like its own suggestions
    ... >> But I think the answer to your original question is that since INT has a ... Loz. ... Prev by Date: ...
    (microsoft.public.vc.language)
  • Re: test whether a double is even?
    ... > On my machine an int is 32 bits, so I would lose precision by converting ... Your original question was about testing a number if it's ... course you may loose precision casting a big number to int, short, or char, ...
    (comp.programming)