Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- B (Bit) API, By Symmetryc
- ase = function(n, b)
- if b==2 then
- local t = string.format("%f", n):find("%.") and {string.format("%f", n):match("(%d*)(.%d*)")} or {n, "0"}
- local stack = {"", ""}
- while t[1]~=0 do
- local char = t[1]%2
- t[1] = math.floor(t[1]/2)
- stack[1] = char..stack[1]
- end
- for i=1, 15-#stack[1] do
- local char = math.floor(t[2]*2)
- t[2] = (t[2]*2)%1
- stack[2] = stack[2]..char
- end
- return tonumber(stack[1].."."..stack[2])
- elseif b==10 then
- n = string.format("%f", n)
- local stack = 0
- local len = n:find("%.") and #(n:match("(.-)%.")) or #n
- local i = len
- for char in n:gmatch("%d") do i = i-1 stack = stack+char*(2^i) end
- return stack
- end
- end
- inary = function(n)
- local mt = {
- __newindex = function(self, key, value) error("attempt to index "..self[1]..", a binary value") end;
- __call = function(self, ...) error("attempt to call "..self[1]..", a binary value") end;
- __metatable = "b";
- __tostring = function(self) return string.format("%f", self[1]) end;
- __unm = function(self) return -self[1] end;
- }
- for k, v in pairs({add="+",sub="-",mul="*",div="/",mod="%",pow="^"}) do
- mt["__"..k] = setfenv(loadstring("return function(l,r) return ase(ase(l[1],10)"..v.."ase(r[1],10),2) end"), getfenv())()
- end
- for k, v in pairs({concat="..",eq="==",lt="<",le="<="}) do
- mt["__"..k] = setfenv(loadstring("return function(l,r) return l[1]"..v.."r[1] end"), getfenv())()
- end
- local t = setmetatable({n}, mt)
- local gate = function(n, a, f) local v = n for i=1, #a do v = f(string.format("%f", v+a[i][1])) end return v end
- t:bnot = function() return tonumber(tostring(self):gsub("0", "O"):gsub("1", "0"):gsub("O", "0") end) end
- t:bor = function(...) return gate(self[1], arg, function(n) return n:gsub("2", "1") end) end
- t:band = function(...) return gate(self[1], arg, function(n) return n:gsub("1", "0"):gsub("2", "1") end) end
- t:bxor = function(...) return gate(self[1], arg, function(n) return n:gsub("2", "0") end) end
- t:bshift = function(n) return self[1]*10^n end
- return t
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement