• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Lua] Bitwise Functions

Forrest

Banned User
Joined
Dec 21, 2016
Messages
57
Reaction score
15
Left Shift
Code:
function lshift(x, y)
  return x * 2 ^ y
end
Example:
Code:
print( lshift(1, 11) ) -- prints 2048

Right Shift
Code:
function rshift(x, y)
  return math.floor(x / 2 ^ y)
end
Example:
Code:
print( rshift(11, 1) ) -- prints 5

Number To Binary
Code:
function getBits(num, asString)
    local x = {}
    while num > 0 do
        rest = num % 2
        table.insert(x, 1, rest)
        num = (num - rest) / 2
    end
    return asString and table.concat(x) or x
end
Example:
Code:
print( getBits(67, true) ) -- prints 1000011

Binary to Number
Code:
function binToNum(binary)
    local bin, sum = tostring(binary):reverse(), 0
    for i = 1, #bin do
        num = bin:sub(i, i) == "1" and 1 or 0
        sum = sum + num * math.pow(2, i - 1)
    end
    return sum
end
Example:
Code:
print( binToNum(1000011) ) -- prints 67

Bitwise Operations
Code:
function bitwise(bits, c, oper)
    local t = {}
    for k, v in pairs(bits) do
        if oper == "and" then
            if (v == 1 and c[k] == 1) then
                table.insert(t, 1)
            else
                table.insert(t, 0)
            end
        elseif oper == "or" then
            if (v == 1 or c[k] == 1) then
                table.insert(t, 1)
            else
                table.insert(t, 0)
            end
        elseif oper == "xor" then
           if ((v == 1 and c[k] ~= 1) or ( c[k] == 1 and v ~= 1) ) then
                table.insert(t, 1)
            else
                table.insert(t, 0)
            end
        elseif oper == "not" then
            table.insert(t, (v == 0 and 1 or 0))
        else
            -- just a default for testing
            table.insert(t, v)
        end
    end
    return table.concat(t)
end
Example:
Code:
local binary = bitwise(getBits(48), getBits(5), "xor")
print(binary, binToNum(binary))
-- prints 011000    24
 
some of these already exist :|
Code:
  [rol] => function: builtin#69
  [rshift] => function: builtin#67
  [ror] => function: builtin#70
  [bswap] => function: builtin#65
  [bxor] => function: builtin#73
  [bor] => function: builtin#72
  [arshift] => function: builtin#68
  [bnot] => function: builtin#64
  [tobit] => function: builtin#63
  [lshift] => function: builtin#66
  [tohex] => function: builtin#74
  [band] => function: builtin#71
 
some of these already exist :|
Code:
  [rol] => function: builtin#69
  [rshift] => function: builtin#67
  [ror] => function: builtin#70
  [bswap] => function: builtin#65
  [bxor] => function: builtin#73
  [bor] => function: builtin#72
  [arshift] => function: builtin#68
  [bnot] => function: builtin#64
  [tobit] => function: builtin#63
  [lshift] => function: builtin#66
  [tohex] => function: builtin#74
  [band] => function: builtin#71
Thanks for the reply but I tried using the bitwise functions in 1.2 and they weren't working for me.
The functions in this thread are not strictly written for TFS they should work with any lua script.

Edit: @Xeraphus can you explain how the built in functions you listed work? It really isn't helpful at all to just say well some of these functions exist already and not give any information beyond that.

That is like me going into threads you either created or are helping in and say oh that code you wrote it has been done before but not giving any more information.
 
Last edited:
who needs a converter from binary to num when you have tonumber?
e.g
Code:
tonumber("1001100", 2)
would print 76 which is right

another example

Code:
tonumber("0x539", 16)
would print 1337
 
who needs a converter from binary to num when you have tonumber?
e.g
Code:
tonumber("1001100", 2)
would print 76 which is right
I do =)

You would be suprised what functions are missing from selective versions/variations of lua.
 
Thanks for the reply but I tried using the bitwise functions in 1.2 and they weren't working for me.
The functions in this thread are not strictly written for TFS they should work with any lua script.

Edit: @Xeraphus can you explain how the built in functions you listed work? It really isn't helpful at all to just say well some of these functions exist already and not give any information beyond that.

That is like me going into threads you either created or are helping in and say oh that code you wrote it has been done before but not giving any more information.
never claimed to know how they work i just said they exist already
if you wanted bitwise functions for lua in general just go to lua 5.3
https://www.lua.org/manual/5.3/manual.html#3.4.2
 
never claimed to know how they work i just said they exist already
if you wanted bitwise functions for lua in general just go to lua 5.3
https://www.lua.org/manual/5.3/manual.html#3.4.2
Again I stress, how about your interpretation; not just a diagram or link to a site. Your level of competence on this language is limited and thats ok but please don't act like you are all knowing and rudly come into another persons thread to give your 1 or 2 line fruitless opinion or a condescending remark and this goes for you as well @tokenzz you are not enlightening anyone by acting in a similar manner.

TLDR if you have nothing good to add to a thread; don't add anything at all.
 
Again I stress, how about your interpretation; not just a diagram or link to a site. Your level of competence on this language is limited and thats ok but please don't act like you are all knowing and rudly come into another persons thread to give your 1 or 2 line fruitless opinion or a condescending remark and this goes for you as well @tokenzz you are not enlightening anyone by acting in a similar manner.

TLDR if you have nothing good to add to a thread; don't add anything at all.
when did i ever act like i was all knowing
me telling you the functions you created already exist isn't an opinion, it's a fact
don't have to snap at me just because you didn't know they existed already ;\
 
when did i ever act like i was all knowing
me telling you the functions you created already exist isn't an opinion, it's a fact
don't have to snap at me just because you didn't know they existed already ;\
I do know they already existed and if you can't determine how you behave on an open forum then maybe you shouldn't be on one to begin with.
 
Again I stress, how about your interpretation; not just a diagram or link to a site. Your level of competence on this language is limited and thats ok but please don't act like you are all knowing and rudly come into another persons thread to give your 1 or 2 line fruitless opinion or a condescending remark and this goes for you as well @tokenzz you are not enlightening anyone by acting in a similar manner.

TLDR if you have nothing good to add to a thread; don't add anything at all.
i'm sorry dear sir, but you gave me a rude response for just pointing out the maybe not so obvious.
 
Back
Top