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 15:15:51 +0100
deivy petrescu wrote on Tue, 7 Jun 2005 22:23:36 -0400:
>on andorxorit(a, b)
> set {a, b} to {characters of (a as string), characters of (b as
>string)}
> if length of a รบ length of b then
> display dialog "no can do!"
> return
> end if
> set andit to {}
> set orit to {}
> set xorit to {}
> repeat with j from 1 to (length of b)
> set {aj, bj} to {(item j of a), (item j of b)}
> if aj is not in {"0", "1"} or bj is not in {"0", "1"} then
> display dialog "no can do!"
> return
> end if
> if aj + bj = 2 then
> set end of andit to "1"
> set end of orit to "1"
> set end of xorit to "0"
> else if aj + bj = 1 then
> set end of andit to "0"
> set end of orit to "1"
> set end of xorit to "1"
> else
> set end of andit to "0"
> set end of orit to "0"
> set end of xorit to "0"
> end if
> end repeat
> 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"
>Tweak as much as you want
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
_______________________________________________
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