Some Bit-Wise Operations Questions for C#

G

Guest

Been a long time since I dealt with some Bit-Wise operations - and never did
so in C#. Can someone help me with the following, simple example and how to
work with each of my situations (listed below)? I'm sure it's really easy
stuff...

Say I have an enum set up as follows:

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

And I have a variable set up as follows:

DaysOfTheWeek SomeDays;

Here are my questions...

1. How do I test if SomeDays contains Friday?

2. How can I write a "for" loop that will go through all of the items in the
enum (in other words Sunday..Saturday) and, for each one, tell me if
"SomeDays" has that day flagged as a "1"?

3. How do I specifically set a flag bit to ZERO ("0")? Say I want to clear
the Saturday bit for "SomeDays". How do I do that?

Thanks for the help!

Alex
 
G

Guest

Alex said:
Been a long time since I dealt with some Bit-Wise operations - and never did
so in C#. Can someone help me with the following, simple example and how to
work with each of my situations (listed below)? I'm sure it's really easy
stuff...

Say I have an enum set up as follows:

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

And I have a variable set up as follows:

DaysOfTheWeek SomeDays;

Here are my questions...

1. How do I test if SomeDays contains Friday?

2. How can I write a "for" loop that will go through all of the items in the
enum (in other words Sunday..Saturday) and, for each one, tell me if
"SomeDays" has that day flagged as a "1"?

3. How do I specifically set a flag bit to ZERO ("0")? Say I want to clear
the Saturday bit for "SomeDays". How do I do that?

For inspiration:

using System;

namespace E
{
[Flags]
public enum DaysOfTheWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
}
public class MainClass
{
public static void Main(string[] args)
{
DaysOfTheWeek dotw = DaysOfTheWeek.Monday | DaysOfTheWeek.Friday;
Console.WriteLine((dotw & DaysOfTheWeek.Thursday) > 0);
Console.WriteLine((dotw & DaysOfTheWeek.Friday) > 0);
foreach(DaysOfTheWeek d in Enum.GetValues(typeof(DaysOfTheWeek)))
{
Console.WriteLine(d + " " + ((dotw & d) > 0));
}
Console.WriteLine((dotw & DaysOfTheWeek.Friday) > 0);
dotw &= ~DaysOfTheWeek.Friday;
Console.WriteLine((dotw & DaysOfTheWeek.Friday) > 0);
Console.ReadLine();
}
}
}

Arne
 
J

Jon Skeet [C# MVP]

Alex Maghen said:
Been a long time since I dealt with some Bit-Wise operations - and never did
so in C#. Can someone help me with the following, simple example and how to
work with each of my situations (listed below)? I'm sure it's really easy
stuff...

Say I have an enum set up as follows:

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

And I have a variable set up as follows:

DaysOfTheWeek SomeDays;

Here are my questions...

Answered in a complete program, on the grounds that it made it easier
to check all the syntax was correct :)

Hope it helps.

using System;

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


class Test
{
static void Main()
{
DaysOfTheWeek someDays = DaysOfTheWeek.Tuesday |
DaysOfTheWeek.Wednesday;

// Prints False
Console.WriteLine (IncludesFriday(someDays));

someDays = DaysOfTheWeek.Sunday |
DaysOfTheWeek.Friday |
DaysOfTheWeek.Saturday;

// Prints True
Console.WriteLine (IncludesFriday(someDays));

// Prints Sunday, Friday, Saturday as True, others False.
DumpDays (someDays);

someDays &= ~DaysOfTheWeek.Sunday;

// Prints Friday, Saturday as True, others False.
DumpDays (someDays);
}

static bool IncludesFriday(DaysOfTheWeek days)
{
return (days & DaysOfTheWeek.Friday) != 0;
}

static void DumpDays (DaysOfTheWeek days)
{
Console.WriteLine();
foreach (DaysOfTheWeek day in
Enum.GetValues(typeof(DaysOfTheWeek)))
{
Console.WriteLine ("{0}? {1}", day,
(days & day) != 0);
}
}
}
 
G

Guest

Thank you both LOTS! Very helpful. Now I just have one more and I'll leave
you alone...

Again, remember that you have the definition...

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

and the variables defined...

DaysOfTheWeek DOTW;
int d;

If the int, d, is the number of the day in the order of the enum (e.g. if
d==3 then it should be the third item in the enum [Tuesday]),

what's the most elegant way to set the bit in my DOTW variable based on the
value of "d"? Should I just be doing powers of 2 math and then casting back
to the variable type?

Alex


Jon Skeet said:
Alex Maghen said:
Been a long time since I dealt with some Bit-Wise operations - and never did
so in C#. Can someone help me with the following, simple example and how to
work with each of my situations (listed below)? I'm sure it's really easy
stuff...

Say I have an enum set up as follows:

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

And I have a variable set up as follows:

DaysOfTheWeek SomeDays;

Here are my questions...

Answered in a complete program, on the grounds that it made it easier
to check all the syntax was correct :)

Hope it helps.

using System;

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


class Test
{
static void Main()
{
DaysOfTheWeek someDays = DaysOfTheWeek.Tuesday |
DaysOfTheWeek.Wednesday;

// Prints False
Console.WriteLine (IncludesFriday(someDays));

someDays = DaysOfTheWeek.Sunday |
DaysOfTheWeek.Friday |
DaysOfTheWeek.Saturday;

// Prints True
Console.WriteLine (IncludesFriday(someDays));

// Prints Sunday, Friday, Saturday as True, others False.
DumpDays (someDays);

someDays &= ~DaysOfTheWeek.Sunday;

// Prints Friday, Saturday as True, others False.
DumpDays (someDays);
}

static bool IncludesFriday(DaysOfTheWeek days)
{
return (days & DaysOfTheWeek.Friday) != 0;
}

static void DumpDays (DaysOfTheWeek days)
{
Console.WriteLine();
foreach (DaysOfTheWeek day in
Enum.GetValues(typeof(DaysOfTheWeek)))
{
Console.WriteLine ("{0}? {1}", day,
(days & day) != 0);
}
}
}
 
J

Jon Skeet [C# MVP]

If the int, d, is the number of the day in the order of the enum (e.g. if
d==3 then it should be the third item in the enum [Tuesday]),

what's the most elegant way to set the bit in my DOTW variable based on the
value of "d"? Should I just be doing powers of 2 math and then casting back
to the variable type?

Assuming you stick to 1, 2, 4, 8 etc, I'd just use bit-shifting:

DaysOfTheWeek dotw = (DaysOfTheWeek) (1<<(d-1));

Alternatively, you could use
Enum.GetValues(typeof(DaysOfTheWeek))[d-1];
 
C

Christof Nordiek

Hi Alex,

another idea to loop over bits:
Say I have an enum set up as follows:

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

for (DaysOfTheWeek day = DaysOfTheWeek.Sunday; day =<
DaysOfTheWeek.Saturday; day<<=1)
{
.....
}

By this you also can loop over a part of the bits, and this also works, if
there are names for combintions.

hth
Christof
 

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