473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Recurring days of week: permutations and bitwise operations.

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"?
Aug 14 '08 #1
4 4140
br********@gmail.com nous disait :
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/
Aug 14 '08 #2
On Aug 14, 10: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"?
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.
Aug 14 '08 #3
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()
Aug 14 '08 #4
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";
?>
Aug 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
3
by: ruben.de.visscher | last post by:
I am trying to write a program that encrypts 8-bit plaintext using a 10-bit key. To generate subkeys, and for other things, i will need to be able to perform permutations on binary strings for...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
1
by: steve | last post by:
Hi All I am writing a program for a gymnasium for membership control It is the first time I have had to deal with appointment diaries and I want to know the best way to store recurring...
5
by: Simon Dean | last post by:
Probably being a little thick here, but when you subtract one date away from another, how do you convert the resultant value into a number of days... I guess I could easily / 60 / 60 / 24... but...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
3
by: hharry | last post by:
Hello All, I'm writing an app to track file transfer activity. I have this enum to represent days of the week: Monday = 1 Tuesday = 2 Wednesday = 3 Thursday = 4
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.