Page 1 of 1

Programming

Posted: Tue Nov 04, 2008 9:12 pm
by Nanti-SARRMM
Fred, my TA's are crazy. If you have two variables while programming in C, X and Y, and you want to exchange their values but cannot use a temporary variable, how would you do it?

I would say something like
int x, y;
{
x=x+y; y=x-y; x=x-y;
}
But the TA's say that would not work with latch numbers, but that somehow doing this works:
int x, y;
{
y=x^y;
x=x^y;
y=x^y;
}
This assumes that x and y are initialized and all that... so am I right or how would it be done?

Posted: Tue Nov 04, 2008 9:24 pm
by Fredjikrang
Sorry, but I don't know squat about programming. I'm learning a little bit, but I'm not into any of that stuff. Yellow would know, or maybe Bismark.

Posted: Tue Nov 04, 2008 10:18 pm
by Giovanni Schwartz
Off topic!

Posted: Tue Nov 04, 2008 10:48 pm
by Fredjikrang
Sorry about that Giovanni! Didn't even realize that it was in the nomination thread! :D

Posted: Wed Nov 05, 2008 12:32 am
by Nanti-SARRMM
is that where i put it? I thought I put it in Random Chatter. Oh well.

Posted: Wed Nov 05, 2008 9:00 am
by bismark
define latch numbers?

the TA works like so (d= decimal, b= binary):

x = 3d = 011b
y = 4d = 100b

y = x ^ y = 111b
x = x ^ y = 100b = 4d
y = x ^ y = 011b = 3d

Posted: Wed Nov 05, 2008 9:40 am
by Nanti-SARRMM
bismark wrote:define latch numbers?
I assume the latch number is the biggest number it can get before it overflows. Thanks for the help though.

Posted: Wed Nov 05, 2008 10:45 am
by bismark
ah.. never heard such a term.

so lets say for a 4 bit number using 2s compliment...

x = 7 = 0111
y = -8 = 1000
x = x + y = 0111 + 1000 = 1111 = -1
y = x - y = 1111 - 1000 = 1111 + 1000 = 0111 = 7
x = x - y = 1111 - 0111 = 1111 + 1001 = 1000 = -8

so that worked.....

x = -1 = 1111
y = -8 = 1000
x = x + y = 1111 + 1000 = 0111 = 7
y = x - y = 0111 - 1000 = 0111 + 1000 = 1111 = -1
x = x - y = 0111 - 1111 = 0111 + 0001 = 1000 = -8

hmm... did they give you the case where it doesn't work?

Posted: Wed Nov 05, 2008 12:35 pm
by 361
Both will work in C/C++ provided you are working with ints or int pointers. (with some caveats I'll point out later)

If you try with other variable types you might get implied type-casting or other un-expected behavior which could screw with your variables values before and after the operation. (like floats / chars / etc...) This is compiler dependent... so what may work on GCC won't behave the same on Intel's compiler...

The XOR method is, however, superior to addition/subtraction in that it can account for overflow...

i.e. If you try this with the following numbers:

x = 4294967295;
y = 1;

x = x + y = 4294967296 (which requires 33-bits to properly represent and will overflow) = ??? (undefined behavior here...)
So if the computer just chops off the 33nd bit... you get 0... so...
x = 0
y = 0 - 1 = -1
x = 0 - (-1) = 1

Using the XOR method we get
x = 11111111111111111111111111111111b
y = 00000000000000000000000000000001b

y = x ^ y = 11111111111111111111111111111110b
x = x ^ y = 00000000000000000000000000000001b
y = x ^ y = 11111111111111111111111111111111b

So although the addition and subtraction will work in most cases... XOR works 100%

I think I may have screwed up that example... but whatever... you get the idea...

Posted: Wed Nov 05, 2008 12:42 pm
by 361
It's also important to note that compilers are smart enough to recognize a temp variable swap pattern and perform optimizations for it...

So by specifying a two variable swap you may actually be making your code less efficient...

Posted: Wed Nov 05, 2008 2:29 pm
by bismark
ah, 361, my bad, i didn't take into account unsigned integers.

so with a 4bit unsigned integers,

x = 1111 = 16
y = 1110 = 15

x = x + y = 1111 + 1110 = 0001 = 1
y = x - y = 0001 - 1110 = 0001 + 0010 = 3
x = x - y = 0001 - 0010 = 0001 + 1110 = 16

so yeah, ugly. assuming signed, 2s complement signed integers, even overflow shouldn't be an issue though as seen in my above example and here:

x = 7 = 0111
y = 1 = 0001

x = x + y = 0111 + 0001 = 1000 = -8
y = x - y = 1000 - 0001 = 1000 + 1111 = 0111 = 7
x = x - y = 1000 - 0111 = 1000 + 1001 = 0001 = 1

of course not all architectures use 2s compliment nor can we assume signed integers, so yes, the XOR is correct.

in C, there should be no difference between using a char and an integer with either of these functions. Floats are a totally different story, and XORing them would definitely not work. I don't feel like figuring out if the adding/subtracting method would.

Posted: Wed Nov 05, 2008 2:39 pm
by 361
The problem with overflows is the undefined behavior

You just can't guarantee it will behave the same...

Posted: Wed Nov 05, 2008 2:52 pm
by bismark
yes, i have a terrible habit of picking terrible examples. and i stand corrected. again.

x = 0111 = 7
y = 0111 = 7

x = x + y = 0111 + 0111 = 1000 = -1
y = x - y = 1000 - 0111 = 1000 + 1001 = 0001 = 1
x = x - y = 1000 - 0001 = 1000 + 1111 = 0111 = 7

Posted: Wed Nov 05, 2008 3:20 pm
by Nanti-SARRMM
Oh.. I get it then. Thanks all.

Posted: Wed Nov 05, 2008 4:25 pm
by Giovanni Schwartz
I can't help. I don't speak nerd.


; )

Posted: Wed Nov 05, 2008 5:03 pm
by Fredjikrang
Don't worry. If you want to be an engineer, you'll learn it soon enough. ;D

Posted: Wed Nov 05, 2008 5:06 pm
by 361
Giovanni Schwartz wrote:I can't help. I don't speak nerd.


; )
It's one of those languages... Minutes to learn... Lifetimes (multiple...) to master...

To begin a strong background in Leetspeak is recommended..

Posted: Wed Nov 05, 2008 5:46 pm
by yellow m&m
I'm going to be learning this soon enough...

Posted: Wed Nov 05, 2008 5:51 pm
by Giovanni Schwartz
101! Now I feel 31337.


(that 101 was lol. Intentionally written to be... Mind blank, can't think of word... Means attempting to be skilled, when your really not.)

Also, if you enjoy physics, you might like flashphysicsgames.com They have some fun ones. And some lame ones.
But mostly fun.