Re: Binary math operations (and, or, etc.)
Re: Binary math operations (and, or, etc.)
- Subject: Re: Binary math operations (and, or, etc.)
- From: deivy petrescu <email@hidden>
- Date: Wed, 8 Jun 2005 14:36:57 -0400
On Jun 8, 2005, at 10:15, Nigel Garvey wrote:
deivy petrescu wrote on Tue, 7 Jun 2005 22:23:36 -0400:
set andit to (reverse of andit) as string
set orit to (reverse of orit) as string
set xorit to (reverse of xorit) as string
return {toand:andit, toor:orit, toxor:xorit}
end andorxorit
Are you sure you mean those reverses, Deivy?
toxor of andorxorit(1110, "1111")
-->"1000"
Well, I meant it but I was wrong! The reverse is wrong
Now, Nigel, my sincerest apologies. How could I've thought of a
script with "script o".
I am truly sorry!
:)
This is a bit faster and can handle bit patterns of unequal length:
on andorxorit(a, b)
script o
property x : a div 1
property y : b div 1
end script
if (a > b) then
set c to a reference to o's x
else
set c to a reference to o's y
end if
set andit to {}
set orit to {}
set xorit to {}
repeat until (c < 1)
set {aj, bj, o's x, o's y} to {(o's x) mod 10, (o's y) mod
10, (o's
x) div 10, (o's y) div 10}
if (aj > 1) or (bj > 1) then
display dialog "no can do!"
return -- something
end if
set sum to (aj + bj) div 1
set beginning of andit to sum div 2
set beginning of orit to (sum + 1) div 2
set beginning of xorit to sum mod 2
end repeat
set andit to andit as string
set orit to orit as string
set xorit to xorit as string
return {toand:andit, toor:orit, toxor:xorit}
end andorxorit
NG
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?
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.
deivy petrescu
email@hidden
_______________________________________________
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