Extracting days of the week from int/binary

H

hharry

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
Friday = 5
Saturday = 6
Sunday = 7

If a file should be transferred on a Monday and Friday, I add 1 to 5
and store 6.
How do I go back the way ?
e.g. I have a value of 6, how to get Monday and Friday ?

Pointers appreciated!
Regards
hharry
 
M

Marc Gravell

You need to use bitwise values, i.e.

[Flags]
public enum Days {
None = 0,
Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8,
Friday = 16, Saturday = 32, Sunday = 64
}

Then "Monday and Friday" (in English) is Days.Monday | Days.Friday

To convert as a number, just cast:
int value = (int) days;
Days days = (Days) value;

To check for individual days:
if((days & Days.Tuesday) == Days.Tuesday)

Marc
 
G

Guest

You have to make the values assigned to particular days subsequent powers of 2
You can then put the [Flags] attribute on the enum:

[Flags]
enum DaysOfWeek {
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64
}


Then you can do sth like this:
DaysOfWeek dd = DaysOfWeek.Monday | DaysOfWeek.Friday;

You can also check if a given value "contains" Monday in it:

if ((dd & DaysOfWeek.Monday) == DaysOfWeek.Monday) { // do sth }


Hope this helps,

_____________
Adam Bieganski
http://godevelop.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top