Advertisement
captmicro

Untitled

Jan 10th, 2013
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.71 KB | None | 0 0
  1. function sizeof(v)
  2.     local retstr = ""
  3.     while (bit.and(v, 0xFF) ~= 0) do
  4.         retstr = retstr .. string.char(bit.band(v, 0xFF))
  5.         v = bit.brshift(v, 8)
  6.     end
  7.     return string.len(retstr)
  8. end
  9.  
  10. --[[---------------
  11. LuaBit v0.4
  12. -------------------
  13. a bitwise operation lib for lua.
  14.  
  15. http://luaforge.net/projects/bit/
  16.  
  17. How to use:
  18. -------------------
  19.  bit.bnot(n) -- bitwise not (~n)
  20.  bit.band(m, n) -- bitwise and (m & n)
  21.  bit.bor(m, n) -- bitwise or (m | n)
  22.  bit.bxor(m, n) -- bitwise xor (m ^ n)
  23.  bit.brshift(n, bits) -- right shift (n >> bits)
  24.  bit.blshift(n, bits) -- left shift (n << bits)
  25.  bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>)
  26.  
  27. Please note that bit.brshift and bit.blshift only support number within
  28. 32 bits.
  29.  
  30. 2 utility functions are provided too:
  31.  bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence)
  32.                -- high bits first
  33.  bit.tonumb(bit_tbl) -- convert a bit table into a number
  34. -------------------
  35.  
  36. Under the MIT license.
  37.  
  38. copyright(c) 2006~2007 hanzhao (abrash_han@hotmail.com)
  39. --]]---------------
  40.  
  41. do
  42.  
  43. ------------------------
  44. -- bit lib implementions
  45.  
  46. local function check_int(n)
  47.  -- checking not float
  48.  if(n - math.floor(n) > 0) then
  49.   error("trying to use bitwise operation on non-integer!")
  50.  end
  51. end
  52.  
  53. local function to_bits(n)
  54.  check_int(n)
  55.  if(n < 0) then
  56.   -- negative
  57.   return to_bits(bit.bnot(math.abs(n)) + 1)
  58.  end
  59.  -- to bits table
  60.  local tbl = {}
  61.  local cnt = 1
  62.  while (n > 0) do
  63.   local last = math.mod(n,2)
  64.   if(last == 1) then
  65.    tbl[cnt] = 1
  66.   else
  67.    tbl[cnt] = 0
  68.   end
  69.   n = (n-last)/2
  70.   cnt = cnt + 1
  71.  end
  72.  
  73.  return tbl
  74. end
  75.  
  76. local function tbl_to_number(tbl)
  77.  local n = table.getn(tbl)
  78.  
  79.  local rslt = 0
  80.  local power = 1
  81.  for i = 1, n do
  82.   rslt = rslt + tbl[i]*power
  83.   power = power*2
  84.  end
  85.  
  86.  return rslt
  87. end
  88.  
  89. local function expand(tbl_m, tbl_n)
  90.  local big = {}
  91.  local small = {}
  92.  if(table.getn(tbl_m) > table.getn(tbl_n)) then
  93.   big = tbl_m
  94.   small = tbl_n
  95.  else
  96.   big = tbl_n
  97.   small = tbl_m
  98.  end
  99.  -- expand small
  100.  for i = table.getn(small) + 1, table.getn(big) do
  101.   small[i] = 0
  102.  end
  103.  
  104. end
  105.  
  106. local function bit_or(m, n)
  107.  local tbl_m = to_bits(m)
  108.  local tbl_n = to_bits(n)
  109.  expand(tbl_m, tbl_n)
  110.  
  111.  local tbl = {}
  112.  local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  113.  for i = 1, rslt do
  114.   if(tbl_m[i]== 0 and tbl_n[i] == 0) then
  115.    tbl[i] = 0
  116.   else
  117.    tbl[i] = 1
  118.   end
  119.  end
  120.  
  121.  return tbl_to_number(tbl)
  122. end
  123.  
  124. local function bit_and(m, n)
  125.  local tbl_m = to_bits(m)
  126.  local tbl_n = to_bits(n)
  127.  expand(tbl_m, tbl_n)
  128.  
  129.  local tbl = {}
  130.  local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  131.  for i = 1, rslt do
  132.   if(tbl_m[i]== 0 or tbl_n[i] == 0) then
  133.    tbl[i] = 0
  134.   else
  135.    tbl[i] = 1
  136.   end
  137.  end
  138.  
  139.  return tbl_to_number(tbl)
  140. end
  141.  
  142. local function bit_not(n)
  143.  
  144.  local tbl = to_bits(n)
  145.  local size = math.max(table.getn(tbl), 32)
  146.  for i = 1, size do
  147.   if(tbl[i] == 1) then
  148.    tbl[i] = 0
  149.   else
  150.    tbl[i] = 1
  151.   end
  152.  end
  153.  return tbl_to_number(tbl)
  154. end
  155.  
  156. local function bit_xor(m, n)
  157.  local tbl_m = to_bits(m)
  158.  local tbl_n = to_bits(n)
  159.  expand(tbl_m, tbl_n)
  160.  
  161.  local tbl = {}
  162.  local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  163.  for i = 1, rslt do
  164.   if(tbl_m[i] ~= tbl_n[i]) then
  165.    tbl[i] = 1
  166.   else
  167.    tbl[i] = 0
  168.   end
  169.  end
  170.  
  171.  --table.foreach(tbl, print)
  172.  
  173.  return tbl_to_number(tbl)
  174. end
  175.  
  176. local function bit_rshift(n, bits)
  177.  check_int(n)
  178.  
  179.  local high_bit = 0
  180.  if(n < 0) then
  181.   -- negative
  182.   n = bit_not(math.abs(n)) + 1
  183.   high_bit = 2147483648 -- 0x80000000
  184.  end
  185.  
  186.  for i=1, bits do
  187.   n = n/2
  188.   n = bit_or(math.floor(n), high_bit)
  189.  end
  190.  return math.floor(n)
  191. end
  192.  
  193. -- logic rightshift assures zero filling shift
  194. local function bit_logic_rshift(n, bits)
  195.  check_int(n)
  196.  if(n < 0) then
  197.   -- negative
  198.   n = bit_not(math.abs(n)) + 1
  199.  end
  200.  for i=1, bits do
  201.   n = n/2
  202.  end
  203.  return math.floor(n)
  204. end
  205.  
  206. local function bit_lshift(n, bits)
  207.  check_int(n)
  208.  
  209.  if(n < 0) then
  210.   -- negative
  211.   n = bit_not(math.abs(n)) + 1
  212.  end
  213.  
  214.  for i=1, bits do
  215.   n = n*2
  216.  end
  217.  return bit_and(n, 4294967295) -- 0xFFFFFFFF
  218. end
  219.  
  220. local function bit_xor2(m, n)
  221.  local rhs = bit_or(bit_not(m), bit_not(n))
  222.  local lhs = bit_or(m, n)
  223.  local rslt = bit_and(lhs, rhs)
  224.  return rslt
  225. end
  226.  
  227. --------------------
  228. -- bit lib interface
  229.  
  230. bit = {
  231.  -- bit operations
  232.  bnot = bit_not,
  233.  band = bit_and,
  234.  bor  = bit_or,
  235.  bxor = bit_xor,
  236.  brshift = bit_rshift,
  237.  blshift = bit_lshift,
  238.  bxor2 = bit_xor2,
  239.  blogic_rshift = bit_logic_rshift,
  240.  
  241.  -- utility func
  242.  tobits = to_bits,
  243.  tonumb = tbl_to_number,
  244. }
  245.  
  246. end
  247.  
  248. --[[
  249. for i = 1, 100 do
  250.  for j = 1, 100 do
  251.   if(bit.bxor(i, j) ~= bit.bxor2(i, j)) then
  252.    error("bit.xor failed.")
  253.   end
  254.  end
  255. end
  256. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement