MeeloJoin Date: 2008-07-04 Post Count: 14763 |
Added operators:
- (Subtraction, Absolute value)
- (Negation, Binary Inversion in this case)
Comparative operators.
print("Meelo's Bit Script Loaded. It really did")
--Positive numbers only pl0x.
local bits = {}
bits.methods = {}
bits.metatable = {} --This will be the metatable for all instances.
bits.metatable.__index = bits.methods
--------------------------------------------METATABLES----------------------------------------------
--Addition
bits.metatable.__add = function(a,b)
local ret = bits.new()
local tab = a.inner
local other = b.inner
if(#other > #tab)then
tab = b.inner
other = a.inner
end
local carry = false
for i,v in ipairs(tab)do
local total = 0
if(v)then --Thanks Lua. You really make me feel good by not allowing arithmetic on booleans :/
total = total + 1
end
if(other[i])then
total = total + 1
end
if(carry)then
total = total + 1
carry = false
end
if(total > 1)then
carry = true
total = total - 2
end
ret:add(total==1)
end
if(carry)then
ret:add(true)
end
return ret
end
--Subtraction [Note, this will get the absolute value]
bits.metatable.__sub = function(a,b)
local ret = bits.new()
local tab = a.inner
local other = b.inner
if(b > a)then
other = a.inner
tab = b.inner
end
local carry = false --Errr... wrong terminology...
for i,v in ipairs(tab)do
local total = 0
if(v)then
total = 1
end
if(other[i])then
total = total - 1
end
if(carry)then
total = total - 1
carry = false
end
if(total < 0)then
carry = true
total = total + 2
end
ret:add(total==1)
end
return ret
end
--Binary inverse.
bits.metatable.__unm = function(a)
local ret = bits.new()
for i,v in ipairs(a.inner)do
ret:add(not v)
end
return ret
end
--Length operator. #blah
bits.metatable.__len = function(a)
return a:tonumber()
end
--Equal to
bits.metatable.__eq = function(a,b)
for i = 1,math.max(#a.inner,#b.inner)do
if(not ((a.inner[i] or 0) == (b.inner[i] or 0)))then
return false
end
end
return true
end
--Less than
bits.metatable.__lt = function(a,b)
local big = math.max(#a.inner,#b.inner)
for i = 0,big-1 do
if(((a.inner[big-i] and 1) or 0) < ((b.inner[big-i] and 1) or 0))then --Spaghetti Code? Nowai!
return true
elseif(((a.inner[big-i] and 1) or 0) > ((b.inner[big-i] and 1) or 0))then
return false
end
end
return false
end
--Less than or equal to.
bits.metatable.__le = function(a,b)
local big = math.max(#a.inner,#b.inner)
for i = 0,big-1 do
if(((a.inner[big-i] and 1) or 0) < ((b.inner[big-i] and 1) or 0))then --Spaghetti Code? Nowai!
return true
elseif(((a.inner[big-i] and 1) or 0) > ((b.inner[big-i] and 1) or 0))then
print(a.inner[big-i],b.inner[big-i])
return false
end
end
return true
end
--tostring
bits.metatable.__tostring = function(a,b)
print(b)
return a:inbase(b)
end
-------------------------------------------METHODS---------------------------------------------------------
--Not useful for user
bits.methods.add = function(me,bool)
table.insert(me.inner,bool)
end
--tonumber method
bits.methods.tonumber = function(me)
local val = 0
local place = 1
for i,v in ipairs(me.inner)do
if(v)then
val = val + place
end
place = place * 2
end
return val
end
--inbase method.
local digits = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
bits.methods.inbase = function(a,base)
base = base or 10
print("BASE: " .. base)
local val = 1
local cur = base
local ret = ""
local digit = 0
for i,v in ipairs(a.inner)do
if(v)then
digit = digit + val
end
val = val * 2
if(val > cur)then
local pos = digit%cur
pos = (pos - (digit%(cur/base)))
ret = digits[pos/(cur/base)+1] .. ret
cur = cur * base
digit = digit - pos
end
end
if(digit>0)then
local pos = digit%cur
pos = (pos - (digit%(cur/base)))
ret = digits[pos/(cur/base)+1] .. ret
end
return ret
end
-------------------------------------------MISC---------------------------------------------------------------
function bits.new(val) --Instance it
val = math.floor(val or 0) --Round it/Make it real
local ret = {}
ret.inner = {}
local place = 2
while val > 0 do --Well, it has to become binary somehow!
local mod = val % place
if(mod == 0)then
table.insert(ret.inner,false)
else
table.insert(ret.inner,true)
end
val = val - mod
place = place * 2
end
setmetatable(ret,bits.metatable)
return ret
end
local num = bits.new(1024)
local num2 = bits.new(4)
print(num,num2,num-num2,num+num2,-num) |