views:

233

answers:

4

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

A: 

Need to use binary...

A = 1, B = 2, C = 4, D = 8, E = 16, F = 32, G = 64, H = 128

This means A + B = 3 but C = 4. You'll never have two conflicting values. I've listed the maximum you can have for a single byte, 8 values or (bits).

Ian
+5  A: 

Binary maths I think. Choose a location that's a power of 2 (1, 2, 4, 8 etch) then you can use the 'bitwise and' operator & to determine the value. Say A = 1, B = 2 , C= 4

00000111 => A B and C => 7

00000101 => A and C => 5

00000100 => C => 4

then to determine them :

if( val  & 4 ) // same as if (C)
if( val  & 2 ) // same as if (B)
if( val  & 1 ) // same as if (A)

if((val  & 4) && (val & 2)  ) // same as if (C and B)

No need for a state table.

Edit: to reflect comment If the tinyint has a maximum value of 255 => you have 8 bits to play with and can store 8 boolean values in there

Preet Sangha
So it's possible to save how many boolean values in an 8-bit tinyint? As many as there are BITS in it: eight. One bit = one value of 1 or 0 which is exactly what is needed for a boolean. (Just thought it would be nice to explain for those who haven't studied this.)
TomA
+1  A: 

binary math as others have said

encoding:

myTinyInt = A*1 + B*2 + C*4 (assuming you convert A,B,C to 0 or 1 beforehand)

decoding

bool A = myTinyInt & 1 != 0 (& is the bitwise and operator in many languages)
bool B = myTinyInt & 2 != 0
bool C = myTinyInt & 4 != 0
Esben Skov Pedersen
+1  A: 

I'll add that you should find a way to not use magic numbers. You can build masks into constants using the Left Logical/Bit Shift with a constant bit position that is the position of the flag of interest in the bit field. (Wow... that makes almost no sense.) An example in C++ would be:

enum Flags {
    kBitMask_A = (1 << 0),
    kBitMask_B = (1 << 1),
    kBitMask_C = (1 << 2),
};

uint8_t byte = 0;        //          byte = 0b00000000
byte |= kBitMask_A;      // Set A,   byte = 0b00000001
byte |= kBitMask_C;      // Set C,   byte = 0b00000101
if (byte & kBitMask_A) { // Test A,  (0b00000101 & 0b00000001) = T
    byte &= ~kBitMask_A; // Clear A, byte = 0b00000100
}

In any case, I would recommend looking for Bitset support in your favorite programming language. Many languages will abstract the logical operations away behind normal arithmetic or "test/set" operations.

D.Shawley