5

I know this is possible, I just simply don't know what its called, and Google hasn't been much help.

Lets say you have 4 options to choose from, A, B, C and D. If a user selects A and C, you can simply save the selection as 5 which = 0101, or 8 if a user selects D (1000).

My question is, how would you convert 5 back into A=1, B=0, C=1, D=0 after it was saved?

I'm trying to save film Age Restrictions, and thought this might be the best way to do it.

Xethron
  • 1,116
  • 9
  • 24
  • I would think of a table with all values in it and do a look up. The question is why do you want to do it in a binary way ? – HamZa May 11 '13 at 13:14
  • I'm looking for an easy way to store a selection of all the different age restrictions, seeing as there is quite a hand full, and I remember a programmer told me about this way a couple of years back, was interested in trying it out and seeing if I can get it to work. Not sure if there is an easier/better way to do it by now? – Xethron May 11 '13 at 13:26
  • 1
    I did see it somewhere here on SO but couldn't find it, but I found [this answer](http://stackoverflow.com/questions/2131758/bitwise-operations-in-php/2131828#2131828) and [this one](http://stackoverflow.com/questions/2773914/c-php-storing-settings-in-an-integer-using-bitwise-operators/2774105#2774105) which seems interesting. – HamZa May 11 '13 at 13:41

3 Answers3

5

I believe the term is BCD (Binary Coded Decimal).

Here is how you could implement it in your situation:

  1. Create 4 Variables (or constants) representing the 4 possible choices, and set their values like this:

    $A = 1;
    $B = 2;
    $C = 4;
    $D = 8;

  2. Then, for each choice from A, B, C, and D, add the value of the associated variable.

After you have stored that combined value, and later you read it back, and you want to decode the choices from the stored value...

To do that, you use the & operator (bitwise AND). For example, suppose the value you have stored and read back into a variable named $Answer is 5.

To decode Answer into the choices like this:

$intChoiceA = ($Answer & 1);
$intChoiceB = ($Answer & 2);
$intChoiceC = ($Answer & 4);
$intChoiceD = ($Answer & 8);

To get the choices as True|False, you can do it like this:

$bChoiceA = ($Answer & 1) > 0;
$bChoiceB = ($Answer & 2) > 0;
$bChoiceC = ($Answer & 4) > 0;
$bChoiceD = ($Answer & 8) > 0;

To get the choices as 0|1, you can do it like this (uses the PHP Ternary Operator:

$intChoiceA = (($Answer & 1) > 0) ? 1 : 0;
$intChoiceB = (($Answer & 2) > 0) ? 1 : 0;
$intChoiceC = (($Answer & 4) > 0) ? 1 : 0;
$intChoiceD = (($Answer & 8) > 0) ? 1 : 0;
Kevin Fegan
  • 1,292
  • 1
  • 16
  • 29
1

By using bitwise operators in PHP you can do it like this:

$A = 1;
$B = 0;
$C = 1;
$D = 0; // input data

$i = $A | ($B<<1) | ($C<<2) | ($D<<3); // joined in one integer

// echo str_pad(decbin($i),4,'0',STR_PAD_LEFT); // bit test ok: 0101 ~ $D$C$B$A

// back conversion:
$A = $i&1;
$B = ($i&2) >>1;
$C = ($i&4) >>2;
$D = ($i&8) >>3;
echo "$A $B $C $D"; // prints "1 0 1 0"
Stano
  • 8,749
  • 6
  • 30
  • 44
  • (also there are many php alternatives on how to convert an integer to boolean ,e.g.: `$A = (boolean)($i&1); $B = (boolean)($i&2); $C = (boolean)($i&4); $D = (boolean)($i&8);` ) – Stano May 11 '13 at 20:46
1

You can use bitwise operations. Essentially, you achieve the effect by doing a bitwise OR on two or more numbers, and then checking if a number was OR'ed in by doing a bitwise AND on the number that you want to test for.

Example:

You want the user to select which languages they are fluent in. The options you present them are English, German, Russian, and Spanish.
First, start by defining the options in powers of 2:

$LANGUAGE_ENGLISH = 2;
$LANGUAGE_RUSSIAN = 4;
$LANGUAGE_GERMAN = 8;
$LANGUAGE_SPANISH = 16;

Let's say the user selects English and Russian. You would combine the options as follows:

$selected_options = 0; // we'll store the result in here
$selected_options = $LANGUAGE_ENGLISH | $LANGUAGE_RUSSIAN;
//It can also be express as:
//$selected_options |= $LANGUAGE_ENGLISH;
//$selected_options |= $LANGUAGE_RUSSIAN;


The $selected_options variable now contains our user's options. To check if an option was stored, you do a bitwise AND. If the result is 0, then the value was not stored. Otherwise, the value was stored.
Example:

if ($selected_options & $LANGUAGE_ENGLISH != 0)
    echo "English was selected";
else
    echo "English was NOT selected";

Requirements:

All numbers need to have non-overlapping bits. The easiest way to prevent overlaps is by only using numbers in powers of 2. For example, in the below table, 3 overlaps with 2, and cannot be used with 2. 1, however, does not overlap with 2 or 4, and can therefore be used with both.

1: 0001
2: 0010
3: 0011
4: 0100

Hope that helps

aggregate1166877
  • 2,196
  • 23
  • 38