views:

60

answers:

3

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"?

+3  A: 

Use a bit masking operator. In the C language:

 X & 8

is true, if the "8"s bit is set.

Ira Baxter
I would take out the "In the C language"... This is now true in most common languages
m_oLogin
mobrule
+3  A: 
if (7 & 1) { // if bit 1 is set in returned number (7)

}
webbiedave
So I would just loop through all the possible 14 outcomes above, and see how many if statements report true? Easy enough. If that's the most efficient way, thanks!
Hallik
+1  A: 

One way would be to loop through your number, left-shifting it (ie divide by 2) and compare the first bit with 1 using the & operand.

m_oLogin