if not modules then modules = { } end modules ['l-bit32'] = {
    version = 1.001,
    license = "the same as regular Lua",
    source  = "bitwise.lua, v 1.24 2014/12/26 17:20:53 roberto",
    comment = "drop-in for bit32, adapted a bit by Hans Hagen",

}

if bit32 then
    return
end

local select = select -- instead of: arg = { ... }

bit32 = {

    bnot = function (a)
        return ~a & 0xFFFFFFFF
    end,

    band = function (x, y, z, ...)
        if not z then
            return ((x or -1) & (y or -1)) & 0xFFFFFFFF
        else
            local res = x & y & z
            for i=1,select("#",...) do
                res = res & select(i,...)
            end
            return res & 0xFFFFFFFF
        end
      end,

    bor = function (x, y, z, ...)
        if not z then
            return ((x or 0) | (y or 0)) & 0xFFFFFFFF
        else
            local res = x | y | z
            for i=1,select("#",...) do
                res = res | select(i,...)
            end
            return res & 0xFFFFFFFF
        end
    end,

    bxor = function (x, y, z, ...)
        if not z then
            return ((x or 0) ~ (y or 0)) & 0xFFFFFFFF
        else
            local res = x ~ y ~ z
            for i=1,select("#",...) do
                res = res ~ select(i,...)
            end
            return res & 0xFFFFFFFF
        end
    end,

    btest = function (x, y, z, ...)
        if not z then
            return (((x or -1) & (y or -1)) & 0xFFFFFFFF) ~= 0
        else
            local res = x & y & z
            for i=1,select("#",...) do
                res = res & select(i,...)
            end
            return (res & 0xFFFFFFFF) ~= 0
        end
    end,

    lshift = function (a, b)
        return ((a & 0xFFFFFFFF) << b) & 0xFFFFFFFF
    end,

    rshift = function (a, b)
        return ((a & 0xFFFFFFFF) >> b) & 0xFFFFFFFF
    end,

    arshift = function (a, b)
        a = a & 0xFFFFFFFF
        if b <= 0 or (a & 0x80000000) == 0 then
            return (a >> b) & 0xFFFFFFFF
        else
            return ((a >> b) | ~(0xFFFFFFFF >> b)) & 0xFFFFFFFF
        end
    end,

    lrotate = function (a ,b)
        b = b & 31
        a = a & 0xFFFFFFFF
        a = (a << b) | (a >> (32 - b))
        return a & 0xFFFFFFFF
    end,

    rrotate = function (a, b)
        b = -b & 31
        a = a & 0xFFFFFFFF
        a = (a << b) | (a >> (32 - b))
        return a & 0xFFFFFFFF
    end,

    extract = function (a, f, w)
        return (a >> f) & ~(-1 << (w or 1))
    end,

    replace = function (a, v, f, w)
        local mask = ~(-1 << (w or 1))
        return ((a & ~(mask << f)) | ((v & mask) << f)) & 0xFFFFFFFF
    end,

}
