Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------
- -- MISC --
- -------------------------------
- --local function to split a string at a given regex
- local function split(str,reg)
- local tbl = {}
- for match in string.gmatch(str,reg) do
- table.insert(tbl,match)
- end
- return tbl
- end
- --Fills up a string with 0000000 until it reaches a specific length
- function fillString(bin,length)
- local output = bin
- while #bin < length do
- table.insert(bin,"0000000")
- end
- return output
- end
- --Fills a char-binary until it fits the "normal" format
- function fillChar(bin)
- local c = bin
- if(#c ~= 7) then
- while #c ~= 7 do
- c = "0"..c
- end
- end
- return c
- end
- -------------------------------
- -- CONVERSION --
- -------------------------------
- -- Get binary from a number
- function numberToBinary(number)
- local s = ""
- repeat
- local remainder = number % 2
- s = remainder..s
- number = (number-remainder)/2
- until number==0
- return s
- end
- -- Get a number from binary input
- function binaryToNumber(bin)
- return tonumber(bin,2)
- end
- -- Get binary from a char
- function charToBinary(ch)
- return numberToBinary(string.byte(string.sub(ch,1,1)))
- end
- -- Get a char from binary
- function binaryToChar(bin)
- return string.char(binaryToNumber(bin))
- end
- -- Get a binary table from a string
- function stringToBinary(str)
- local tbl = {}
- for char in string.gmatch(str,".") do
- tbl[#tbl+1] = charToBinary(char)
- end
- return tbl
- end
- -- Get a string from a binary table
- function binaryToString(bin)
- str = ""
- for k,v in pairs(bin) do
- str = str..binaryToChar(v)
- end
- return str
- end
- -------------------------------
- -- LOGIC --
- -- SIMPLE --
- -------------------------------
- -- 'not' a binary sequence
- function bnot(bin)
- local bin = split(bin,".")
- local output = ""
- for k,v in pairs(bin) do
- output = output..(v=="1" and "0" or "1")
- end
- return output
- end
- -- 'or' two binary sequences
- function bor(first,second)
- local f,s = split(first,"."),split(second,".")
- local output = ""
- if(#first ~= #second) then error("binaries are not of the same length") end
- for i = 1, #first do
- output = output..((f[i] == "1" or s[i] == "1") and "1" or "0")
- end
- return output
- end
- -- 'and' two binary sequences
- function band(first,second)
- local f,s = split(first,"."),split(second,".")
- local output = ""
- if(#first ~= #second) then error("binaries are not of the same length") end
- for i = 1, #first do
- output = output..((f[i] == "1" and s[i] == "1") and "1" or "0")
- end
- return output
- end
- -- 'Äquivalenz'
- function bxand(first,second)
- local f,s = split(first,"."),split(second,".")
- local output = ""
- if(#first ~= #second) then error("binaries are not of the same length") end
- for i = 1, #first do
- output = output..((f[i] == s[i]) and "1" or "0")
- end
- return output
- end
- -- 'Antivalenz'
- function bxor(first,second)
- local f,s = split(first,"."),split(second,".")
- local output = ""
- if(#first ~= #second) then error("binaries are not of the same length") end
- for i = 1, #first do
- output = output..((f[i] ~= s[i]) and "1" or "0")
- end
- return output
- end
- -------------------------------
- -- LOGIC --
- -- TABLE --
- -------------------------------
- function btnot(bin)
- local output = {}
- for k,v in pairs(bin) do
- table.insert(output,bnot(v))
- end
- return output
- end
- -- 'or' two binary sequences
- function btor(first,second)
- local output = {}
- for k,v in pairs(first) do
- table.insert(output,bor(v,second[k]))
- end
- return output
- end
- -- 'and' two binary sequences
- function btand(first,second)
- local output = {}
- for k,v in pairs(first) do
- table.insert(output,band(v,second[k]))
- end
- return output
- end
- -- 'Äquivalenz'
- function btxand(first,second)
- local output = {}
- for k,v in pairs(first) do
- table.insert(outp,bxand(v,second[k]))
- end
- return output
- end
- -- 'Antivalenz'
- function btxor(first,second)
- local output = {}
- for k,v in pairs(first) do
- table.insert(output,bxor(v,second[k]))
- end
- return output
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement