A Generalized Way of Testing Bits in "[Flags]"

G

Guest

Let's say I have an Enum:
[Flags]
public enum DaysOfTheWeek : short
{
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday = 0x8,
Thursday = 0x10,
Friday = 0x20,
Saturday = 0x40
}

and I want to create two functions:

public bool ContainsDayGet(int DayFromSundayZero)
public bool ContainsDaySet(int DayFromSundayZero, bool NewValue)

one will test the appropriate bit and return true if that bit is set (false
otherwise). The other will set the appropriate bit.

I want to figure out a way to do this in a more generic way by looking into
the way the enum was defined at runtime instead of having to write a
switch-case for each of the potential options. I guess this means that I want
to "index" my way into an enum. Can one do that?

Any help on the construction of these two functions in a generaic sense
would be most helpful.

Thanks.

Alex Maghen
 
T

Terry Rogers

Let's say I have an Enum:
[Flags]
public enum DaysOfTheWeek : short
{
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday = 0x8,
Thursday = 0x10,
Friday = 0x20,
Saturday = 0x40
}

and I want to create two functions:

public bool ContainsDayGet(int DayFromSundayZero)
public bool ContainsDaySet(int DayFromSundayZero, bool NewValue)

one will test the appropriate bit and return true if that bit is set (false
otherwise). The other will set the appropriate bit.

I want to figure out a way to do this in a more generic way by looking into
the way the enum was defined at runtime instead of having to write a
switch-case for each of the potential options. I guess this means that I want
to "index" my way into an enum. Can one do that?

Any help on the construction of these two functions in a generaic sense
would be most helpful.

Thanks.

Alex Maghen

I think you want something like this:

public static class DaysOfTheWeekTools
{
public static bool ContainsDayGet(DaysOfTheWeek value,
DaysOfTheWeek day)
{
return (value & day) == day;
}

public static DaysOfTheWeek ContainsDaySet(DaysOfTheWeek current,
DaysOfTheWeek day, bool value)
{
if(value)
{
return current | day;
}
else
{
return current & (~day);
}
}
}

Then you can do the following:

// create a enumeration
DaysOfTheWeek myDays = DaysOfTheWeek.Saturday | DaysOfTheWeek.Sunday;

Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Sunday)); // true
Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Monday)); // false

// turn on a flag
myDays = DaysOfTheWeekTools.ContainsDaySet(myDays,
DaysOfTheWeek.Monday, true);

Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Sunday)); // true
Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Monday)); // true

// turn off a flag
myDays = DaysOfTheWeekTools.ContainsDaySet(myDays,
DaysOfTheWeek.Sunday, false);

Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Sunday)); // false
Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Monday)); // true

hth
Terry
 
T

Tiberiu Covaci

I think that what he is actaually after is something like that:

public bool ContainsDayGet(int DayFromSundayZero)
{
return this.m_daysOfTheWeek & (1<< DayFromSundayZero )) != 0;
}

public bool ContainsDaySet(int DayFromSundayZero, bool NewValue )
{
bool oldValue = ContainsDayGet(DayFromSundayZero)
if(oldValue != NewValue){
if(NewValue)
{
this.m_daysOfTheWeek | = 1<<DayFromSundayZero;
}
else
{
this.m_daysOfTheWeek & = ~(1<<DayFromSundayZero);
}
}
return oldValue;
}


Regards,

Tibi
MCT, MCPD
Terry Rogers said:
Let's say I have an Enum:
[Flags]
public enum DaysOfTheWeek : short
{
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday = 0x8,
Thursday = 0x10,
Friday = 0x20,
Saturday = 0x40
}

and I want to create two functions:

public bool ContainsDayGet(int DayFromSundayZero)
public bool ContainsDaySet(int DayFromSundayZero, bool NewValue)

one will test the appropriate bit and return true if that bit is set
(false
otherwise). The other will set the appropriate bit.

I want to figure out a way to do this in a more generic way by looking
into
the way the enum was defined at runtime instead of having to write a
switch-case for each of the potential options. I guess this means that I
want
to "index" my way into an enum. Can one do that?

Any help on the construction of these two functions in a generaic sense
would be most helpful.

Thanks.

Alex Maghen

I think you want something like this:

public static class DaysOfTheWeekTools
{
public static bool ContainsDayGet(DaysOfTheWeek value,
DaysOfTheWeek day)
{
return (value & day) == day;
}

public static DaysOfTheWeek ContainsDaySet(DaysOfTheWeek current,
DaysOfTheWeek day, bool value)
{
if(value)
{
return current | day;
}
else
{
return current & (~day);
}
}
}

Then you can do the following:

// create a enumeration
DaysOfTheWeek myDays = DaysOfTheWeek.Saturday | DaysOfTheWeek.Sunday;

Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Sunday)); // true
Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Monday)); // false

// turn on a flag
myDays = DaysOfTheWeekTools.ContainsDaySet(myDays,
DaysOfTheWeek.Monday, true);

Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Sunday)); // true
Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Monday)); // true

// turn off a flag
myDays = DaysOfTheWeekTools.ContainsDaySet(myDays,
DaysOfTheWeek.Sunday, false);

Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Sunday)); // false
Console.WriteLine(DaysOfTheWeekTools.ContainsDayGet(myDays,
DaysOfTheWeek.Monday)); // true

hth
Terry
 

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

Top