Connecting Tech Pros Worldwide Help | Site Map

Recurring days of week: permutations and bitwise operations.

bretonlais@gmail.com
Guest
 
Posts: n/a
#1: Aug 14 '08
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"?
Nicolas Bouthors
Guest
 
Posts: n/a
#2: Aug 14 '08

re: Recurring days of week: permutations and bitwise operations.


bretonlais@gmail.com nous disait :
Quote:
How do I decode 0000 1011 back to "Sunday, Monday, Wednesday"?
Here's a dirty way. You probably have tens of nicer ways that escape
me right now.

$str = ( ($value & 1)?"Monday ":"" )
.( ($value & 2)?"Tuesday ":"" )
.( ($value & 4)?"Wednesday ":"" )
...

Cheers,

Nico


--
Nicolas - 06 20 71 62 34 - http://nicolas.bouthors.org/album/
The Hajj
Guest
 
Posts: n/a
#3: Aug 14 '08

re: Recurring days of week: permutations and bitwise operations.


On Aug 14, 10:25 am, bretonl...@gmail.com wrote:
Quote:
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"?
use an array() ("0001" ="Sunday Sunday Sunday", yada yada)

or just use the date object functionability , or use decimal, why
convert back in forth? make more code.
The Hajj
Guest
 
Posts: n/a
#4: Aug 14 '08

re: Recurring days of week: permutations and bitwise operations.


You might be better of storing the information as a time reference,
use can extract the day information from there by doing date_create.
This will help if you want to run it on a specific day as you have the
reference for it, you could go further if you wanted to add the time.

looking mktime()
matthewconcha@gmail.com
Guest
 
Posts: n/a
#5: Aug 14 '08

re: Recurring days of week: permutations and bitwise operations.


On Aug 14, 7:25*am, bretonl...@gmail.com wrote:
Quote:
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";


?>
Closed Thread