Nathan Nathan - 1 year ago 93
PHP Question

Most efficient way to extract bit flags

I have these possible bit flags.

1, 2, 4, 8, 16, 64, 128, 256, 512, 2048, 4096, 16384, 32768, 65536

So each number is like a true/false statement on the server side. So if the first 3 items, and only the first 3 items are marked "true" on the server side, the web service will return a 7. Or if all 14 items above are true, I would still get a single number back from the web service which is is the sum of all those numbers.

What is the best way to handle the number I get back to find out which items are marked as "true"?

Answer Source
if (7 & 1) { // if bit 1 is set in returned number (7)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download