I’ve got this small question – given a bitmask of weekdays (e.g., Sunday = 0x01, Monday = 0x02, Tuesday = 0x04, etc…) and today’s day (in a form of Sunday = 1, Monday = 2, Tuesday = 3, etc…) – what’s the most elegant way to find out the next day from today, that’s set in the bitmask? By elegant I mean, is there a way to do this without if/switch/etc…, because I know the non-elegant way?
Edit I probably should’ve mentioned (to make this more clear) that the variable holding the bitmask can have several of the days set, so for example (roughly):
uDay = Sunday | Monday; today = Tuesday;
I need to get ‘Sunday’
So that’s just one if at the beginning and while loop. How’s that?
Edit: I just realized there was a degenerate case where if the use passes today>=14 (or greater than the highest bit set) the while loop becomes infinite. The (today % 7) on line 4 fixes this case.
And if I may grouse (light-heartedly) about the other version getting the checkmark, my version only have 2 modulus calls, while the checked solution will have a minimum of 1 and a maximum of 6 modulus calls.
Also, the comment about does the function return ‘today’ if today is set is interesting. If the function should not return today unless today is the only day in the set would require that you pre-increment today on line 3 of my solution.