Re: Need XOR solution
Re: Need XOR solution
- Subject: Re: Need XOR solution
- From: Nigel Garvey <email@hidden>
- Date: Thu, 8 Aug 2002 02:05:43 +0100
"Marc K. Myers" wrote on Wed, 07 Aug 2002 16:13:17 -0400:
>
>
>
> You could use the brilliant "Fast ASCII" coverter from Arthur's script to
>
> get the number value of each byte, and XOR them together with this:
>
>
>
> on Xorify(n1, n2)
>
> set xor to 0
>
> repeat 8 times
>
> if n1 mod 2 is not equal to n2 mod 2 then set xor to xor + 256
>
> set n1 to n1 div 2
>
> set n2 to n2 div 2
>
> set xor to xor div 2
>
> end repeat
>
> -- return xor
>
> end Xorify
>
>
That works beautifully, but even after "walking" through the eight
>
iterations with pencil and paper I still didn't understand *how* it
>
works. Honestly, I was amazed that I got the right answer after the
>
eighth pass through the loop! Could you explain it, briefly, for those
>
of us who are less mathematically inclined?
Oh gosh. At this time of night.... :-)
Basically, when you divide a number (actually an integer, I suppose) by
2, you're effectually shifting its binary bit pattern one place to the
right:
00001100 -- 12
00000110 -- 12 div 2 = 6
00000011 -- 6 div 2 = 3
When you modulate that number by two, the result will be either 0 or 1 -
that is, the value of the lowest-order bit, which is the one on the
extreme right.
00001100 -- 12 mod 2 = 0
00000110 -- 6 mod 2 = 0
00000011 -- 3 mod 2 = 1
The mathematical method is a convenient way of looking at the individual
bits one by one. Modulate the number by 2 and the result is value of the
lowest bit. Divide the number by 2 and the bits all shift one position to
the right. Modulate again by 2 to find the value of the bit that's just
move into the lowest position.
Bitwise XOR means: if bit n of one bit pattern is different from bit n of
the other, set bit n of the result to 1. If the two bits have the same
value, clear the result bit. (Set it to 0.)
>
> on Xorify(n1, n2)
>
> set xor to 0
My method starts out with all the bits of 'xor' set to 0.
>
> repeat 8 times
>
> if n1 mod 2 is not equal to n2 mod 2 then set xor to xor + 256
For each bit of the eight in each byte, the 'mod' operation gets the
values of the lowest-order bits of n1 and n2. If these are the same,
nothing happens. If they're different, 256 is added to 'xor'. Although
we're thinking 8-bit here, integers have more bits than that. Adding 256
to XOR sets its *9th* bit (called "bit 8") to 1. (More properly, it
*adds* the binary pattern for 256 - a 1 followed by 8 zeros - to a value
whose 9th bit is known to be zero.)
>
> set n1 to n1 div 2
>
> set n2 to n2 div 2
>
> set xor to xor div 2
The three binary patterns are then each shifted one place to the right.
This brings the second-lowest bits of n1 and n2 into the lowest
positions, and the 9th bit of 'xor' moves into the 8th position. By the
end of eight repeats, it's migrated right through to the rightmost
position, followed by the bits that were either set or not on subsequent
repeats. Thus the lowest order bit of 'xor' now represents the result of
the XOR between the lowest order bits of the two numbers - and so on
respectively for the other seven bits.
>
> end repeat
>
> -- return xor
>
> end Xorify
Hope that's clear, not too simple, and/or not too complicated. ;-)
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.