Advertisement
snowgum

encodemask

Dec 30th, 2022 (edited)
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.67 KB | None | 0 0
  1. #!/usr/bin/lua
  2.  
  3. function hasbit(x, p)
  4.     return x % (p + p) >= p
  5. end
  6. function bitor(x, y)
  7.     local p = 1; local z = 0; local limit = x > y and x or y
  8.     while p <= limit do
  9.         if hasbit(x, p) or hasbit(y, p) then
  10.             z = z + p
  11.         end
  12.         p = p + p
  13.     end
  14.     return z
  15. end
  16.  
  17. mtab = {}
  18. vtab = {1, 2, 4, 8}
  19.  
  20. for i = 1, 32 do
  21.     mtab[i] = 0
  22. end
  23.  
  24. numarg = #arg
  25. for argval = 1, numarg do
  26.     band = arg[argval]
  27.     if tonumber(band) <= 128 then
  28.         idx = math.floor((band - 1) / 4) + 1
  29.         idxr = 33 - idx
  30.         val = vtab[(band - ((idx - 1) * 4 ))]
  31.         mtab[idxr] = bitor(mtab[idxr], val)
  32.     end
  33. end
  34. for i = 1, 32 do
  35.     mtab[i] = string.format("%X", mtab[i])
  36. end
  37.  
  38. print(table.concat(mtab))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement