Advertisement
gocha

Lua 5.1 logical right shift (32bit)

Mar 16th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.50 KB | None | 0 0
  1.     -- logical right shift (32bit)
  2.     local rshift = function(value, amount)
  3.         if amount == 0 then
  4.             return value
  5.         end
  6.         if amount < 0 then
  7.             error("illegal argument #2: shift amount must be greater than zero.")
  8.         end
  9.  
  10.         -- first logical right shift
  11.         if value < 0 then
  12.             assert(value >= -0xffffffff)
  13.             value = 0x80000000 + math.floor(value / 2)
  14.         else
  15.             value = math.floor(value / 2)
  16.         end
  17.         -- and more
  18.         for i = 2, amount do
  19.             value = math.floor(value / 2)
  20.         end
  21.         return value
  22.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement