将十进制数转换成旗帜值

我有一些限制,如下:

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

如何从十进制数确定标志?

点赞
用户142162
用户142162

为了确定设置了哪些标志,你需要使用按位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
    --执行一些有趣的和局部连接的操作
end
2014-02-17 23:03:31