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

  • Home
  • SEARCH
  • 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 6636545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:11:59+00:00 2026-05-25T23:11:59+00:00

I have a bit shift mask that represents days in a week: Sunday =

  • 0

I have a bit shift mask that represents days in a week:

Sunday = 1
Monday = 2
Tuesday = 4
...
Saturday = 64

I’m using a bitmask because several (at least one) days may be set to 1.

The problem

Then I get a date. Any date. And based on the date.DayOfWeek I need to return the first nearest date after it that is set in the bitmask. So my method can return the same day or any other day between date and date + 6.

Example 1

My bitmask defines all days being set to 1. In this case my method should return the same date, because date.DayOfWeek is set in the bitmask.

Example 2

My bitmask defines that only Wednesday is set to 1. If my incoming date is Tuesday, I should return date+1 (which is Wednesday). But if incoming date is Thursday I should return date+6 (which is again Wednesday).

Question

What is the fastest and most elegant way of solving this? Why also fastest? Because I need to run this several times so if I can use some sort of a cached structure to get dates faster it would be preferred.

Can you suggest some guidance to solve this in an elegant way? I don’t want to end up with a long spaghetti code full of ifs and switch-case statements…

Important: It’s important to note that bitmask may be changed or replaced by something else if it aids better performance and simplicity of code. So bitmask is not set in stone…

A possible approach

It would be smart to generate an array of offsets per day and save it in a private class variable. Generate it once and reuse it afterwards like:

return date.AddDays(cachedDayOffsets[date.DayOfWeek]);

This way we don’t use bitmask at all and the only problem is how to generate the array the fastest and with as short code as possible.

  • 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-05-25T23:11:59+00:00Added an answer on May 25, 2026 at 11:11 pm

    You might hate this answer, but perhaps you’ll be able to run with it in a new direction. You said performance is extremely important, so maybe it’s best to just index all the answers up front in some data structure. That data structure might be somewhat convoluted, but it could be encapsulated in its own little world and not interfere with your main code. The data structure I have in mind would be an array of ints. If you allow Monday, Friday, and Saturday, those ints would be:

    [1][0][3][2][1][0][0]
    

    Ok weird right? This is basically the “days away” list for the week. On Sunday, there’s “1 day until next allowed day of week”. On Monday, there’s 0. On Tuesday, it’s 3 days away. Now, once you build this list, you can very easily and very quickly figure out how many days you need to add to your date to get the next occurance. Hopefully this helps??

    Generating these offsets

    This is the code that generates these offsets:

    this.dayOffsets = new int[] {
        this.Sundays ? 0 : this.Mondays ? 1 : this.Tuesdays ? 2 : this.Wednesdays ? 3 : this.Thursdays ? 4 : this.Fridays ? 5 : 6,
        this.Mondays ? 0 : this.Tuesdays ? 1 : this.Wednesdays ? 2 : this.Thursdays ? 3 : this.Fridays ? 4 : this.Saturdays ? 5 : 6,
        this.Tuesdays ? 0 : this.Wednesdays ? 1 : this.Thursdays ? 2 : this.Fridays ? 3 : this.Saturdays ? 4 : this.Sundays ? 5 : 6,
        this.Wednesdays ? 0 : this.Thursdays ? 1 : this.Fridays ? 2 : this.Saturdays ? 3 : this.Sundays ? 4 : this.Mondays ? 5 : 6,
        this.Thursdays ? 0 : this.Fridays ? 1 : this.Saturdays ? 2 : this.Sundays ? 3 : this.Mondays ? 4 : this.Tuesdays ? 5 : 6,
        this.Fridays ? 0 : this.Saturdays ? 1 : this.Sundays ? 2 : this.Mondays ? 3 : this.Tuesdays ? 4 : this.Wednesdays ? 5 : 6,
        this.Saturdays ? 0 : this.Sundays ? 1 : this.Mondays ? 2 : this.Tuesdays ? 3 : this.Wednesdays ? 4 : this.Thursdays ? 5 : 6
    };
    

    This one generates forward offsets. So for any given date you can get the actual applicable date after it by simply:

    SomeDate.AddDays(this.dayOffsets[(int)SomeDate.DayOfWeek]);
    

    If you need to get nearest past date you can reuse the same array and calculate it out by using this formula:

    SomeDate.AddDays((this.dayOffsets[(int)SomeDate.DayOfWeek] - 7) % 7);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of code that basically reads an XML document using the
i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have a bit of code that passes around a ton of objects and
I have a bit of code that looks like this: text = reg.Replace(text, new
I have written an interpreter that requires me to perform 32-bit division of unsigned
I have a question regarding bit masks and shift operator in C uint32_t reg_val
I have problem when try to shift bit for a negative number with Perl.
I have a bit questions regarding the way we do Coding CSS. Probably because
Lets say that I have an array of 4 32-bit integers which I use
I have learned how to work with 80x86 assembler, so in bit-wise shift operation,

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.