For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > swapping how does it work?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author swapping how does it work?
Jacky Luk

2005-06-10, 8:59 am

(a)^=(b); (b)^=(a); (a)^=(b);

I'm pretty weak in mathematics,
but what's going on inside these statements to achieve swapping of a and b?
Thanks
Jack


lallous

2005-06-10, 8:59 am

Hello,

If you just expand it a little, it would have became clear:

> (a)^=(b); (b)^=(a); (a)^=(b);


a = a ^ b;
b = b ^ a -> b = b ^ a ^ b -> b = b ^ b ^ a -> b = 0 ^ a -> b = a;
a = a ^ b -> a = a ^ b ^ a -> a = a ^ a ^ b -> a = 0 ^ b -> a = b;

Thus swapped.

--
Elias
"Jacky Luk" <jl@knight.com> wrote in message
news:Oy2J%23lYbFHA.3912@TK2MSFTNGP15.phx.gbl...
> (a)^=(b); (b)^=(a); (a)^=(b);
>
> I'm pretty weak in mathematics,
> but what's going on inside these statements to achieve swapping of a and
> b?
> Thanks
> Jack
>



Jacky Luk

2005-06-10, 8:59 am


"lallous" <lallous@lgwm.org> 撰寫於郵件新聞:e%23HiQwYbFHA.2984@TK2MSFTNGP15.phx.gbl...
> Hello,
>
> If you just expand it a little, it would have became clear:
>
>
> a = a ^ b;
> b = b ^ a -> b = b ^ a ^ b -> b = b ^ b ^ a -> b = 0 ^ a -> b = a;

------------------------------
how come b^b = 0, not b2 (square)?


> a = a ^ b -> a = a ^ b ^ a -> a = a ^ a ^ b -> a = 0 ^ b -> a = b;
>
> Thus swapped.
>
> --
> Elias
> "Jacky Luk" <jl@knight.com> wrote in message
> news:Oy2J%23lYbFHA.3912@TK2MSFTNGP15.phx.gbl...
>
>



lallous

2005-06-10, 8:59 am

The "^" is the XOR operator in C like programming languages.

--
Elias

"Jacky Luk" <jl@knight.com> wrote in message
news:OKRY%238YbFHA.2796@TK2MSFTNGP10.phx.gbl...[color=darkred]
>
> "lallous" <lallous@lgwm.org>
> 撰寫於郵件新聞:e%23HiQwYbFHA.2984@TK2MSFTNGP15.phx.gbl...
> ------------------------------
> how come b^b = 0, not b2 (square)?
>
>


Andy Sinclair

2005-06-10, 8:59 am

Jacky Luk wrote:
>how come b^b = 0, not b2 (square)?


Because the ^ operator is bitwise XOR.

Andy
Lawrence Groves

2005-06-10, 8:59 am

"lallous" <lallous@lgwm.org> wrote in message
news:e%23HiQwYbFHA.2984@TK2MSFTNGP15.phx.gbl...
> Hello,
>
> If you just expand it a little, it would have became clear:
>
>
> a = a ^ b;
> b = b ^ a -> b = b ^ a ^ b -> b = b ^ b ^ a -> b = 0 ^ a -> b = a;
> a = a ^ b -> a = a ^ b ^ a -> a = a ^ a ^ b -> a = 0 ^ b -> a = b;
>
> Thus swapped.


Absolutely fabulous explanation!!!!!!

If I was 'pretty weak' in maths like Jacky, I'd be on the floor now.


Here's a simpler example (in binary, given ^ is the XOR operator):

a = 1011
b = 0110

a^=b (a is now 1101)
b^=a (b is now 1011)
a^=b (a is now 0110)

a=0110
b=1011 (values swapped)

Loz.


Roy Fine

2005-06-10, 4:03 pm

since the operator ^ is a bitwise operator, you can develop the proof based
on single bit values, then extend it to any bit width operands.

consider the following three step sequence

operation a b
---------- ----- ------
initial 0 1
a ^= b 1 1
b ^= a 1 0
a ^= b 1 0

operation a b
---------- ----- ------
initial 1 0
a ^= b 1 0
b ^= a 1 1
a ^= b 0 1

operation a b
---------- ----- ------
initial 1 1
a ^= b 0 1
b ^= a 1 0
a ^= b 1 1

step 1. (a) will be 0 if the two were originally equal, else (a) will be 1

step 2. (b) will be changed if they were not originally equal

step 3. (a) will be !b if they were originall not equal, else b

after the first set (a) is a flag that determines whether the two bits were
originally equal - now you have the original value of (b) and a flag that
indicates whether the two were originaly equal. the next two step set the
new values of (b) then (a). I first used the method to swap the contents of
the two registers - I was programming in BAL on an IBM 370/165, and accesses
to main memory were VERY slow.

regards
roy fine




"Lawrence Groves" <lgroves@ducost.deleteme.com> wrote in message
news:OUa1%23zZbFHA.2768@tk2msftngp13.phx.gbl...
> "lallous" <lallous@lgwm.org> wrote in message
> news:e%23HiQwYbFHA.2984@TK2MSFTNGP15.phx.gbl...
>
> Absolutely fabulous explanation!!!!!!
>
> If I was 'pretty weak' in maths like Jacky, I'd be on the floor now.
>
>
> Here's a simpler example (in binary, given ^ is the XOR operator):
>
> a = 1011
> b = 0110
>
> a^=b (a is now 1101)
> b^=a (b is now 1011)
> a^=b (a is now 0110)
>
> a=0110
> b=1011 (values swapped)
>
> Loz.
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com