1

我有一些这样的限制:

interesting = 0x1
choked = 0x2
remote_interested = 0x4
remote_choked = 0x8
supports_extensions = 0x10
local_connection = 0x20
handshake = 0x40
connecting = 0x80
queued = 0x100
on_parole = 0x200
seed = 0x400
optimistic_unchoke = 0x800
rc4_encrypted = 0x100000
plaintext_encrypted = 0x200000

并且文档告诉我'标志属性告诉你对等体处于哪个状态。它设置为上面枚举的任意组合'所以基本上我调用 dll 并用代表标志值的十进制数字填充结构,举几个例子:

2086227
170
2098227
106

我如何从小数点确定标志?

4

1 回答 1

4

为了确定设置了哪些标志,您需要使用按位与运算(bit32.band()在 Lua 5.2 中)。例如:

function hasFlags(int, ...)
    local all = bit32.bor(...)
    return bit32.band(int, all) == all
end

if hasFlags(2086227, interesting, local_connection) then
    -- do something that has interesting and local_connection
end
于 2014-02-17T23:03:31.380 回答