Re: Binary math operations (and, or, etc.)
Re: Binary math operations (and, or, etc.)
- Subject: Re: Binary math operations (and, or, etc.)
- From: has <email@hidden>
- Date: Wed, 8 Jun 2005 18:38:57 +0100
Stephen Jonke wrote:
>Bitwise operations it is.
AppleScript doesn't provide built-in bitwise operators, so you'll need to provide your own. Also, be aware that AppleScript AS integers are only 30 bits long; this could be an issue for you depending on what data you're processing.
-- PRIVATE
on _OP(a, b, obj, opName)
try
considering punctuation, hyphens and white space
set a to a as integer
set b to b as integer
end considering
set rm to obj's op(a < 0, b < 0)
if a < 0 then
set a to bNOT(a)
set am to 1
else
set am to 0
end if
if b < 0 then
set b to bNOT(b)
set bm to 1
else
set bm to 0
end if
set res to 0
repeat with e from 0 to 28
if obj's op(((a + am) mod 2) as boolean, ((b + bm) mod 2) as boolean) then
set res to res + 2 ^ e
end if
set a to a div 2
set b to b div 2
end repeat
if rm then
return (res - 2 ^ 29) as integer -- AS integers are only 30 bits long, including sign
else
return res
end if
on error eMsg number eNum
error "Can't " & opName & ": " & eMsg number eNum
end try
end _OP
-- PUBLIC
on bNOT(a)
try
considering punctuation, hyphens and white space
return -(a as integer) - 1
end considering
on error eMsg number eNum
error "Can't bNOT: " & eMsg number eNum
end try
end bNOT
on bAND(a, b)
script obj
on op(a, b)
return a and b
end op
end script
return _OP(a, b, obj, "bAND")
end bAND
on bOR(a, b)
script obj
on op(a, b)
return a or b
end op
end script
return _OP(a, b, obj, "bOR")
end bOR
on bXOR(a, b)
script obj
on op(a, b)
return (a or b) and not (a and b)
end op
end script
return _OP(a, b, obj, "bXOR")
end bXOR
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
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