Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum programming issue

Tags:

c#

.net

enums

I have a code like

enum WeekDays
{
    Sat = 64,
    Sun = 1,     
    Mon = 2,
    Tue = 4,
    Wed = 8,
    Thu = 16,
    Fri = 32
    WorkDays = Sat | Sun | Mon | Tue | Wed 
}

I would like to know more about:

WorkDays = Sat | Sun | Mon | Tue | Wed 

What does its value mean?

like image 387
AshOoO Avatar asked Jun 04 '26 00:06

AshOoO


1 Answers

You probably have a [Flags] attribute above that.

Workdays is created as the binary-or of the working day values (not my working days).

So the days are manually numbered to make them powers of 2 :

Sun  = 0000001
Mon  = 0000010
Tue  = 0000100
Wed  = 0001000
Sat  = 1000000

etc

And then you can use binary operators to do Set operations:

MyWeekend = Sat | Sun;  // 1000000 | 0000001 = 1000001

and use the binary-and to test membership:

WeekDays d = ...;
if ((d & MyWeekend) != 0)
{
    // it's weekend !
}
like image 182
Henk Holterman Avatar answered Jun 05 '26 12:06

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!