未经签名而无需比较签名

Eci*_*ana 1 optimization lua casting bit-manipulation unsigned-integer

要将32位无符号转换为有符号整数,可以使用:

function convert(n)
    if n >= 2 ^ 31 then
        return n - 2 ^ 32
    end
    return n
end
Run Code Online (Sandbox Code Playgroud)

没有这种比较,是否可以做到这一点?

PS:这是Lua,因此我不能像C一样"演员".

aka*_*ice 5

也许你可以通过位操作来做到这一点.在Smalltalk中将是:

^self - (2*(self bitAnd: 16r80000000))
Run Code Online (Sandbox Code Playgroud)

显然bita在Lua中不是原生的,但是现在可以使用各种位库,请参阅http://lua-users.org/wiki/BitwiseOperators

一旦找到合适的位和功能,就可以了

return n - bitand(n,MAXINT)*2
Run Code Online (Sandbox Code Playgroud)