Programming

Any miscellaneous posts can live here.
Post Reply
Nanti-SARRMM
Posts: 1958
Joined: Thu Apr 03, 2008 10:02 pm
Location: Beyond the Mountains of the Copper Miners into the Desert of Absolute Boredom
Contact:

Programming

Post 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?
Fredjikrang
Never Coming Back?
Posts: 2031
Joined: Mon Apr 02, 2007 9:59 am
Location: Provo, UT
Contact:

Post 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.
[img]http://fredjikrang.petfish.net/Fence-banner.png[/img]
User avatar
Giovanni Schwartz
Posts: 3396
Joined: Wed Mar 19, 2008 9:41 pm

Post by Giovanni Schwartz »

Off topic!
Fredjikrang
Never Coming Back?
Posts: 2031
Joined: Mon Apr 02, 2007 9:59 am
Location: Provo, UT
Contact:

Post by Fredjikrang »

Sorry about that Giovanni! Didn't even realize that it was in the nomination thread! :D
[img]http://fredjikrang.petfish.net/Fence-banner.png[/img]
Nanti-SARRMM
Posts: 1958
Joined: Thu Apr 03, 2008 10:02 pm
Location: Beyond the Mountains of the Copper Miners into the Desert of Absolute Boredom
Contact:

Post by Nanti-SARRMM »

is that where i put it? I thought I put it in Random Chatter. Oh well.
bismark
Old Man
Posts: 723
Joined: Fri Jul 13, 2007 10:36 am
Contact:

Post 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
Nanti-SARRMM
Posts: 1958
Joined: Thu Apr 03, 2008 10:02 pm
Location: Beyond the Mountains of the Copper Miners into the Desert of Absolute Boredom
Contact:

Post 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.
bismark
Old Man
Posts: 723
Joined: Fri Jul 13, 2007 10:36 am
Contact:

Post 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?
361
Posts: 194
Joined: Thu Sep 25, 2008 12:58 pm

Post 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...
361
Posts: 194
Joined: Thu Sep 25, 2008 12:58 pm

Post 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...
bismark
Old Man
Posts: 723
Joined: Fri Jul 13, 2007 10:36 am
Contact:

Post 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.
361
Posts: 194
Joined: Thu Sep 25, 2008 12:58 pm

Post by 361 »

The problem with overflows is the undefined behavior

You just can't guarantee it will behave the same...
bismark
Old Man
Posts: 723
Joined: Fri Jul 13, 2007 10:36 am
Contact:

Post 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
Nanti-SARRMM
Posts: 1958
Joined: Thu Apr 03, 2008 10:02 pm
Location: Beyond the Mountains of the Copper Miners into the Desert of Absolute Boredom
Contact:

Post by Nanti-SARRMM »

Oh.. I get it then. Thanks all.
User avatar
Giovanni Schwartz
Posts: 3396
Joined: Wed Mar 19, 2008 9:41 pm

Post by Giovanni Schwartz »

I can't help. I don't speak nerd.


; )
Fredjikrang
Never Coming Back?
Posts: 2031
Joined: Mon Apr 02, 2007 9:59 am
Location: Provo, UT
Contact:

Post by Fredjikrang »

Don't worry. If you want to be an engineer, you'll learn it soon enough. ;D
[img]http://fredjikrang.petfish.net/Fence-banner.png[/img]
361
Posts: 194
Joined: Thu Sep 25, 2008 12:58 pm

Post 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..
User avatar
yellow m&m
The Yellow One
Posts: 649
Joined: Mon May 21, 2007 10:01 am
Location: my parents attic
Contact:

Post by yellow m&m »

I'm going to be learning this soon enough...
Staple guns: because duct tape can't make that "kaCHUNK" noise
User avatar
Giovanni Schwartz
Posts: 3396
Joined: Wed Mar 19, 2008 9:41 pm

Post 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.
Post Reply