On Aug 14, 7:25*am, bretonl...@gmail.com wrote:

I am creating a scheduling application which will allow users to

schedule recurring tasks, e.g., the user can set a task to run (M,T,W)

or (M,T,W,TH,F,S) etc. I want binary encode each day of the week and

use a bitwise OR and AND to determine which days the user selected

(and all the permutations):

* * const None = 0x0;

* * const Sunday = 0x1;

* * const Monday = 0x2;

* * const Tuesday = 0x4;

* * const Wednesday = 0x8;

* * const Thursday = 0x10;

* * const Friday = 0x20;

* * const Saturday = 0x40;

If the user selects (Sunday(0000 0001), Monday (0000 0010), Wednesday

(0000 1000) the bits set would be: 0000 1011 = "decimal 11"

How do I decode 0000 1011 back to "Sunday, Monday, Wednesday"?

Looks like I am going to answer my own questions (Thanks to David for

some clarification on bitwise operations).

<?php

/**

* Encode

*/

define("Sunday",0x1);

define("Monday",0x2);

define("Tuesday",0x4);

define("Wednesday",0x8);

define("Thursday",0x10);

define("Friday",0x20);

define("Saturday",0x40);

// Sunday = 1, Monday = 2

// will echo, "is Sunday, is Monday."

$encoded = 3;

/**

* Decode

*/

$isSunday = $encoded & Sunday;

$isMonday = $encoded & Monday;

$isTuesday = $encoded & Tuesday;

$isWednesday = $encoded & Wednesday;

$isThursday = $encoded & Thursday;

$isFriday = $encoded & Friday;

$isSaturday = $encoded & Saturday;

if($isSunday==Sunday) echo "is Sunday \n";

if($isMonday==Monday) echo "is Monday \n";

if($isTuesday==Tuesday) echo "is Tuesday \n";

if($isWednesday==Wednesday) echo "is Wednesday \n";

if($isThursday==Thursday) echo "is Thursday \n";

if($isFriday==Friday) echo "is Friday \n";

if($isSaturday==Saturday) echo "is Saturday \n";

?>