Question

I have some constraints like so:

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

and the documentation tells me 'The flags attribute tells you in which state the peer is in. It is set to any combination of the enums above' so basically I call the dll and it fills in the structure with a decimal number representing the flag values, a few examples:

2086227
170
2098227
106

How do I from the decimal determine the flags?

Was it helpful?

Solution

In order to determine which flags were set, you need to use the bitwise AND operation (bit32.band() in Lua 5.2). For example:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top