将十进制数转换为标志值

时间:2014-02-17 22:49:16

标签: lua libtorrent

我有一些限制:

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

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

2086227
170
2098227
106

我如何从小数确定标志?

1 个答案:

答案 0 :(得分:4)

为了确定设置了哪些标志,您需要使用bitwise AND操作(Lua 5.2中的bit32.band())。例如:

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