Re: swapping how does it work?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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@xxxxxxxxxxxxxxxxxxx> wrote in message
news:OUa1%23zZbFHA.2768@xxxxxxxxxxxxxxxxxxxxxxx
> "lallous" <lallous@xxxxxxxx> wrote in message
> news:e%23HiQwYbFHA.2984@xxxxxxxxxxxxxxxxxxxxxxx
> > 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.
>
> 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.
>
>


.



Relevant Pages

  • Re: Arch specific mmap attributes (Was: mprotect pgprot handling weirdness)
    ... vm_map_attributes flag, and add the necessary bits and pieces to ... I dislike to add separate flags member into vma. ... request for example a relaxed ordering mapping on x86 might be useful. ... I think it basically boils down to either extend vm_flags to always be ...
    (Linux-Kernel)
  • Re: New uploaded form marked by green flag.
    ... But,I want to extend the no. of days. ... Currently!New Tag appears for ... In my domain,that green flag remains for 2 days and then it ...
    (microsoft.public.sharepoint.portalserver)
  • Re: New uploaded form marked by green flag.
    ... The link which you have sent, mentions about how to disable!New Tag. ... But,I want to extend the no. of days. ... In my domain,that green flag remains for 2 days and then it ...
    (microsoft.public.sharepoint.portalserver)
  • Re: New uploaded form marked by green flag.
    ... The link which you have sent, mentions about how to disable!New Tag. ... But,I want to extend the no. of days. ... In my domain,that green flag remains for 2 days and then it ...
    (microsoft.public.sharepoint.portalserver)
  • Re: NASM 0.98.39 vs. NASM 2.03.01 disassembly
    ... "Executable code segment. ... The flag is called the D flag and it indicates the ... default length for effective addresses and operands referenced by ... overrides) is one way to change the ...
    (alt.lang.asm)