Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:42:21+00:00 2026-06-09T10:42:21+00:00

I have a method that converts a bitmask to a list of days using

  • 0

I have a method that converts a bitmask to a list of days using an Enum. I am trying to do the reverse, but am having trouble. I have DaysOfWeekToEnum working, but not DaysOfWeekFromEnum. Below is what I am trying to do. Can anyone help?

public static short DaysOfWeekFromEnum(IEnumerable<DaysInWeekIds> daysOfWeek)
{
    short mask;

    foreach (var item in daysOfWeek)
    {
        mask &= item; // ????
    }

    return mask
}

public static IEnumerable<DaysInWeekIds> DaysOfWeekToEnum(short mask)
{
    var values = new List<DaysInWeekIds>();

    foreach (short enumValue in Enum.GetValues(typeof(DaysInWeekIds)))
    {
        if (mask & enumValue == enumValue)
        {
            values.Add((DaysInWeekIds) Enum.ToObject(typeof(DaysInWeekIds),
                enumValue));
        }
    }

    return values;
}

/// <summary> The days in week ids. </summary>
public enum DaysInWeekIds : short
{
    M = 1,
    Tu = 2,
    W = 4,
    Th = 8,
    F = 16,
    Sa = 32,
    Su = 64
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:42:23+00:00Added an answer on June 9, 2026 at 10:42 am

    It should be |=, not &=, to set the additional bits.

    You could also join string representations together, and pass the result to Enum.Parse method. This is not as efficient, but the code will look shorter.

    var res = (DaysInWeekIds)Enum.Parse(
        typeof(DaysInWeekIds)
    ,   string.Join(",", daysOfWeek.Select(d => d.ToString()).ToArray())
    );
    

    This returns 25 for {M, Th, F} (link to ideone).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that converts an int to a base60 string (using 0-9,
I have the following extension method that takes a List and converts it to
I have and old(ish) C# method I wrote that takes a number and converts
I am trying to write a method that converts from engineering notation to double
I have this method that converts a signed or non-signed byte to int ,
I have this little crazy method that converts BigDecimal values into nice and readable
I have a method that takes a mac address from a string and converts
I have the following method that takes in a details object, validates it, converts
I have a method that converts a file to bytes so that I can
I have a method that converts a URL entered/pasted into a micropost form by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.