Three boolean values saved in one tinyint

Posted by Casper on Stack Overflow See other posts from Stack Overflow or by Casper
Published on 2009-10-01T11:14:17Z Indexed on 2010/05/29 21:02 UTC
Read the original article Hit count: 186

Filed under:

Hello,

probably a simple question but I seem to be suffering from programmer's block. :)

I have three boolean values: A, B, and C. I would like to save the state combination as an unsigned tinyint (max 255) into a database and be able to derive the states from the saved integer. Even though there are only a limited number of combinations, I would like to avoid hard-coding each state combination to a specific value (something like if A=true and B=true has the value 1).

I tried to assign values to the variables so (A=1, B=2, C=3) and then adding, but I can't differentiate between A and B being true from i.e. only C being true.

I am stumped but pretty sure that it is possible. Thanks

© Stack Overflow or respective owner

Related posts about type-conversion