bitwise Operators
Sean Shao
shaosean at hotmail.com
Thu Nov 24 05:34:34 EST 2005
Due to the fact that the bitwise operators in RunRev do not work on signed
integers, nor do they have shiftLeft and shiftRight, I decided to write my
own, so here they are.. I'll put them on my website another day.. As always,
these are released into the Public Domain.. Any questions, comments,
suggestions feel free to email me..
All parameters are signed integers (ie. 200, -9, 32)
function bitwiseAnd p1, p2 -- in other languages ( 9 & 2 )
local tP1Neg, tP2Neg
if (p1 < 0) then
put bitNot abs(p1) +1 into p1
put TRUE into tP1Neg
end if
if (p2 < 0) then
put bitNot abs(p2) +1 into p2
put TRUE into tP2Neg
end if
if (tP1Neg) AND (tP2Neg) then
return 0 - bitNot ((p1 bitAnd p2) -1)
else return p1 bitAnd p2
end bitwiseAnd
function bitwiseOr p1, p2 -- in other languages ( 9 | 2 )
local tP1Neg, tP2Neg
if (p1 < 0) then
put bitNot abs(p1) +1 into p1
put TRUE into tP1Neg
end if
if (p2 < 0) then
put bitNot abs(p2) +1 into p2
put TRUE into tP2Neg
end if
if (tP1Neg) OR (tP2Neg) then
return 0 - bitNot ((p1 bitOr p2) -1)
else return p1 bitOr p2
end bitwiseOr
function bitwiseXor p1, p2 -- in other languages ( 9 ^ 2 )
local tP1Neg, tP2Neg
if (p1 < 0) then
put bitNot abs(p1) +1 into p1
put TRUE into tP1Neg
end if
if (p2 < 0) then
put bitNot abs(p2) +1 into p2
put TRUE into tP2Neg
end if
if (tP1Neg) AND (tP2Neg) then
return p1 bitXor p2
else if (tP1Neg) OR (tP2Neg) then
return 0 - bitNot ((p1 bitXor p2) -1)
else return p1 bitXor p2
end bitwiseXor
function bitwiseNot p1 -- in other languages ( ~9 )
return 0 - (p1 + 1)
end bitwiseNot
### parameter p2 is the amount to shift over
function bitwiseShiftLeft p1, p2 -- in other languages ( 9 << 2)
return p1 * (2 ^ p2)
end bitwiseShiftLeft
function bitwiseShiftRight p1, p2 -- in other languages ( 9 >> 2 )
if (p2 > 0) then return trunc(p1 / (2 ^ p2))
if (p2 = 0) then return p1
if (p1 < 0) then return trunc(p1 / (2 ^ p2)) -1
end bitwiseShiftRight
function bitwiseShiftRightZero p1, p2 -- in other languages ( 9 >>> 2 )
if (p1 > 0) then return trunc(p1 / (2 ^ p2))
if (p2 = 0) then return p1
if (p1 < 0) then
put baseConvert(bitNot abs(p1) +1, 10, 2) into p1
repeat p2
delete char -1 of p1
put 0 before p1
end repeat
return baseConvert(p1, 2, 10)
end if
end bitwiseShiftRightZero
_________________________________________________________________
Dont just search. Find. Check out the new MSN Search!
http://search.msn.click-url.com/go/onm00200636ave/direct/01/
More information about the use-livecode
mailing list