-1

I have a non-zero integer value which is the sum of the days of the week where the days have the following values:

Sunday = 1 

Monday = 2

Tuesday = 4

Wednesday= 8 

Thursday = 16 

Friday = 32 

Saturday = 64

Example: Integer value 127 - All days of week, 65- only on sunday and saturday

I have seen few posts Convert integer to a list of week days regarding this, but I could not find any of the code in Java.

Depending on integer number i need to schedule task on particular day (sunday- saturday)

Community
  • 1
  • 1

1 Answers1

0

Are you asking how to determine which days were selected based on the value 127? If so, you can tell whether a given day was selected by doing an AND:

int inputValue = 127;
if (Monday & inputValue) {
  // Monday was selected
}

etc.

Hard to be more specific without knowing your question.

Lolo
  • 3,935
  • 5
  • 40
  • 50
  • My question is depeding on integer value, I need to schedule a task. I need to write a algorithm with function Power(2,i) and do some calculations and convert that integer value to the week days. Example : if integer value is 3- then scheduled on sunday and monday, 33- friday and sunday, 7- sunday,monday and tuesday. – user2208325 Mar 25 '13 at 17:34
  • can you see my above question and if you find any solution, it would be helpful – user2208325 Mar 25 '13 at 18:34
  • @user2208325 I believe I answered just this. If you need to decide when something is scheduled for, say, Monday, the answer is: boolean isScheduledForMonday=Monday&inputValue; You have already defined your constant values Monday, Tuesday, ... etc as powers of two, so there is no need to invoke any Power(2,i) math function here. Concretely, here: 3&1 and 3&2 will be true (1 and 2) respectively, so Sunday and Monday are matches. But 3&4, 3&8, ..., 3&64 return false (0) so you know the other days aren't scheduled when inputValue is 3. – Lolo Mar 25 '13 at 18:40
  • Note: if you wanted to make the power of two apparent in the expressions of your days, you could have Sunday = 1<<0; Monday = 1<<1; ... Saturday = 1<<7; No need to invoke the math power function for this. – Lolo Mar 25 '13 at 18:44
  • @Lolo- Can you mention in detail? I dont understand how can 3&1 and 3&2 will be true – user2208325 Mar 25 '13 at 19:24
  • @user2208325 then you need to read more about bitwise operators, frankly. think about the binary representations of 1,2 and 3 – Peter Elliott Mar 25 '13 at 21:57
  • @user2208325 I agree with Peter. Briefly: x&1 tests whether the first bit of x is set to 1 or 0. x&2 tests whether the second bit is set and so on. So if x&1 is true, it means that Sunday was selected. – Lolo Mar 26 '13 at 03:30