[Lua] Bitwise Functions

Discussion in 'Mods & Lua Functions' started by Forrest, Dec 22, 2016.

  1. Forrest

    Forrest Banned User

    Joined:
    Dec 21, 2016
    Messages:
    58
    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
    
     
    Joe Rod and Tarek1337 like this.
  2. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,723
    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
     
    Joe Rod, Tarek1337 and cbrm like this.
  3. Forrest

    Forrest Banned User

    Joined:
    Dec 21, 2016
    Messages:
    58
    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: Dec 22, 2016
    Tarek1337 likes this.
  4. tokenzz

    tokenzz :thinking:

    Joined:
    Feb 2, 2013
    Messages:
    703
    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
     
    Joe Rod and Tarek1337 like this.
  5. Forrest

    Forrest Banned User

    Joined:
    Dec 21, 2016
    Messages:
    58
    I do =)

    You would be suprised what functions are missing from selective versions/variations of lua.
     
    Tarek1337 likes this.
  6. tokenzz

    tokenzz :thinking:

    Joined:
    Feb 2, 2013
    Messages:
    703
    of course you do codex. I am always surprised of your arrogance :D
     
    Tarek1337 likes this.
  7. Forrest

    Forrest Banned User

    Joined:
    Dec 21, 2016
    Messages:
    58
    I am not codex but nice try, close but no cigar :)
     
    Tarek1337 likes this.
  8. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,723
    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
     
  9. Forrest

    Forrest Banned User

    Joined:
    Dec 21, 2016
    Messages:
    58
    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.
     
  10. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,723
    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 ;\
     
    StreamSide likes this.
  11. Forrest

    Forrest Banned User

    Joined:
    Dec 21, 2016
    Messages:
    58
    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.
     
  12. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,723
    ill leave
    sorry boss
     
    Tarek1337 and tokenzz like this.
  13. tokenzz

    tokenzz :thinking:

    Joined:
    Feb 2, 2013
    Messages:
    703
    i'm sorry dear sir, but you gave me a rude response for just pointing out the maybe not so obvious.
     
    Tarek1337 and Xeraphus like this.

Share This Page