[UPD] bitwise math functions (v1.2) -- long post

Sean Shao shaosean at hotmail.com
Mon Nov 28 17:41:50 CST 2005


Well after a few more days of working on the bitwise math stuff I finally 
have it working.. It's probably not 100% when working outside of 32-bit 
numbers, but it tries.. All shifts are between 0-31 (32 is the same as 0).. 
I tested against the JavaScript answers and they are identical. Thanks to 
everyone on the Chatrev server for helping, testing and putting up with me 
;-)

Here are the functions:

function bitwiseAnd p1, p2     -- other languages (&)
  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     -- other languages (|)
  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     -- other languages (^)
  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     -- other languages (~)
  return 0 - (p1 + 1)
end bitwiseNot


function bitwiseShiftLeft p1, p2     -- other languages (<<)
  local tP1Neg
  put (abs(p2) MOD 32) into p2
  if (p2 = 0) then return p1     -- no shifting
  if (p1 < 0) then put TRUE into tP1Neg
  put baseConvert(abs(p1), 10, 2) into p1
  repeat p2
    put 0 after p1
  end repeat
  if (tP1Neg) then
    if (baseConvert(p1, 2, 10) > 2147483648) then return 2147483648 - 
(baseConvert(p1, 2, 10) MOD 2147483648)
    else return 0 - baseConvert(p1, 2, 10)
  else
    if (baseConvert(p1, 2, 10) >= 2147483648) then return 0 - (2147483648 - 
baseConvert(char -31 to -1 of p1, 2, 10))
    else return baseConvert(p1, 2, 10)
  end if
end bitwiseShiftLeft


function bitwiseShiftRight p1, p2     -- other languages (>>) and (>>>)
  # There is a third optional parameter which will force left-pad the binary 
number with 0's
  # This in effect switches negative numbers into positive
  # bitwiseShiftRight(13, 4)     -- other languages (>>)
  # bitwiseShiftRight(13, 4, TRUE)     -- other languages (>>>)

  local tP1Neg, tPad, tZeroFill
  put (abs(p2) MOD 32) into p2
  if (paramCount() = 3) then put TRUE into tZeroFill
  if (p1 = 0) then return p1
  if (p1 > 0) then
    if (p2 = 0) then return p1     -- no shifting
    put baseConvert(abs(p1), 10, 2) into p1
    repeat p2
      delete char -1 of p1
      if (tZeroFill) then put 0 before p1
    end repeat
    return baseConvert(p1, 2, 10)
  end if
  if (p1 < 0) then
    if (tZeroFill) then
      put baseConvert(bitNot abs(p1) +1, 10, 2) into p1
      repeat p2
        delete char -1 of p1
        put 0 before p1
      end repeat
      put abs(baseConvert(p1, 2, 10)) into p1
      if (p1 = 0) then return 0
      else return p1
    else
      if (p2 = 0) then return p1     -- no shifting
      put abs(p1) / (2 ^ p2) into p1
      if (p1 is an integer) then return 0 - p1
      return 0 - trunc(p1) - 1
    end if
  end if
end bitwiseShiftRight


function bitwiseRotateLeft p1, p2
  local tSL, tSR
  put bitwiseShiftLeft(p1, p2) into tSL
  put bitwiseShiftRight(p1, abs(p2)) into tSR
  return bitwiseOr(tSL, tSR)
end bitwiseRotateLeft

function bitwiseRotateRight p1, p2
  local tSR, tSL
  put bitwiseShiftRight(p1, p2) into tSR
  put bitwiseShiftLeft(p1, 32-p2) into tSL
  return bitwiseOR(tSR, tSL)
end bitwiseRotateRight


--
-Sean <www.shaosean.tk>
1f u c4n r34d th15 u n33d 2 g3t l41d

_________________________________________________________________
Don’t 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