summaryrefslogtreecommitdiff
path: root/lualibs-number.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lualibs-number.lua')
-rw-r--r--lualibs-number.lua82
1 files changed, 79 insertions, 3 deletions
diff --git a/lualibs-number.lua b/lualibs-number.lua
index a1249f0..a4dbe3b 100644
--- a/lualibs-number.lua
+++ b/lualibs-number.lua
@@ -6,11 +6,15 @@ if not modules then modules = { } end modules ['l-number'] = {
license = "see context related readme files"
}
-local tostring = tostring
-local format, floor, insert, match = string.format, math.floor, table.insert, string.match
+-- this module will be replaced when we have the bit library
+
+local tostring, tonumber = tostring, tonumber
+local format, floor, match, rep = string.format, math.floor, string.match, string.rep
+local concat, insert = table.concat, table.insert
local lpegmatch = lpeg.match
-number = number or { }
+number = number or { }
+local number = number
-- a,b,c,d,e,f = number.toset(100101)
@@ -56,3 +60,75 @@ function number.bits(n,zero)
end
return t
end
+
+--~ http://ricilake.blogspot.com/2007/10/iterating-bits-in-lua.html
+
+function number.bit(p)
+ return 2 ^ (p - 1) -- 1-based indexing
+end
+
+function number.hasbit(x, p) -- typical call: if hasbit(x, bit(3)) then ...
+ return x % (p + p) >= p
+end
+
+function number.setbit(x, p)
+ return (x % (p + p) >= p) and x or x + p
+end
+
+function number.clearbit(x, p)
+ return (x % (p + p) >= p) and x - p or x
+end
+
+--~ function number.tobitstring(n)
+--~ if n == 0 then
+--~ return "0"
+--~ else
+--~ local t = { }
+--~ while n > 0 do
+--~ insert(t,1,n % 2 > 0 and 1 or 0)
+--~ n = floor(n/2)
+--~ end
+--~ return concat(t)
+--~ end
+--~ end
+
+function number.tobitstring(n,m)
+ if n == 0 then
+ if m then
+ rep("00000000",m)
+ else
+ return "00000000"
+ end
+ else
+ local t = { }
+ while n > 0 do
+ insert(t,1,n % 2 > 0 and 1 or 0)
+ n = floor(n/2)
+ end
+ local nn = 8 - #t % 8
+ if nn > 0 and nn < 8 then
+ for i=1,nn do
+ insert(t,1,0)
+ end
+ end
+ if m then
+ m = m * 8 - #t
+ if m > 0 then
+ insert(t,1,rep("0",m))
+ end
+ end
+ return concat(t)
+ end
+end
+
+--~ print(number.tobitstring(8))
+--~ print(number.tobitstring(14))
+--~ print(number.tobitstring(66))
+--~ print(number.tobitstring(0x00))
+--~ print(number.tobitstring(0xFF))
+--~ print(number.tobitstring(46260767936,8))
+--~ print(#number.tobitstring(46260767936,6))
+
+function number.valid(str,default)
+ return tonumber(str) or default or nil
+end