Re: Binary math operations (and, or, etc.)
Re: Binary math operations (and, or, etc.)
- Subject: Re: Binary math operations (and, or, etc.)
- From: "Nigel Garvey" <email@hidden>
- Date: Wed, 8 Jun 2005 21:43:41 +0100
deivy petrescu wrote on Wed, 8 Jun 2005 14:36:57 -0400:
>Now, Nigel, my sincerest apologies. How could I've thought of a
>script with "script o".
>I am truly sorry!
>:)
Me too. ;-) It wasn't even for speed this time, either.
>Actually I like your script better than mine. Yyour xorit is
>actually good for any numbers of terms.
>First question, did you run your script, can you use sum as a variable?
Yeah. No problem. Do you have a terminology clash?
>Second, using different lengths is not good. Example "00111" and
>"01111" whatever is the operation you want a five digits number which
>you will not get with your script. You would get an error message
>otherwise.
I'm not getting any error messages, but I am getting four-digit results -
which is probably not what would be required.
Since posting, I've guessed that Stephen would probably want to use
actual numbers - and in fact he's e-mailed me off-list to that effect -
so I've rewritten it again. In this version, numbers are treated as
numbers, strings as bit patterns. It needs a bit more work, and probably
separation into separate handlers for each "operator".
on andorxorit(a, b)
set {aDvdr, targetWidth} to {2, 0}
if a's class is in {string, Unicode text} then set {aDvdr,
targetWidth} to {10, (count a)}
set bDvdr to 2
if b's class is in {string, Unicode text} then
set bDvdr to 10
set w to (count b)
if w > targetWidth then set targetWidth to w
end if
set {andN, orN, xorN} to {0, 0, 0}
set {andit, orit, xorit} to {{}, {}, {}}
set p to 1
repeat until ((a is 0) and (b is 0))
set {aj, bj} to {a mod aDvdr, b mod bDvdr}
if ((aj > 1) or (bj > 1)) then
display dialog "no can do!"
return -- something
end if
tell (aj + bj) div 1
tell (it div 2) to set {andN, beginning of andit} to {andN + p *
it, it}
tell ((it + 1) div 2) to set {orN, beginning of orit} to {orN + p
* it, it}
tell (it mod 2) to set {xorN, beginning of xorit} to {xorN + p *
it, it}
end tell
set {a, b, p} to {a div aDvdr, b div bDvdr, p * 2}
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set padWidth to targetWidth - (count andit)
if padWidth > 0 then
set zeros to text 1 thru padWidth of "0000000000000000000000000000"
set {andit, orit, xorit} to {zeros & andit, zeros & orit, zeros & xorit}
else
set {andit, orit, xorit} to {andit as string, orit as string, xorit
as string}
end if
set AppleScript's text item delimiters to astid
return {toand:{andN, andit}, toor:{orN, orit}, toxor:{xorN, xorit}}
end andorxorit
andorxorit(15, "00101")
--> {toand:{5, "00101"}, toor:{15, "01111"}, toxor:{10, "01010"}}
andorxorit(15, 5)
--> {toand:{5, "0101"}, toor:{15, "1111"}, toxor:{10, "1010"}}
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden