简体   繁体   English

需要排列和组合

[英]Permutation and combination required

I am trying to read the "day" value from native calendar of blackberry,the value is returned as a integer ,which is mapped to a value for each day of the week.The values are like : 我试图从黑莓的本机日历中读取“日”值,该值以整数形式返回,它被映射到一周中每一天的值。值如下:

  • Monday:32768 星期一:32768
  • tue: 16384 星期二:16384
  • wed :8192 结婚:8192
  • thur :4096 thur:4096
  • fri :2048 星期五:2048
  • sat :1024 坐:1024
  • sun :65536 太阳:65536

I am able to see if the value is mon/tue/wed/thu/fri/sat/sun if the event is occuring for a single day using 如果事件发生一天,我能看到值是mon / tue / wed / thu / fri / sat / sun

if (rule.MONDAY == rule.getInt(rule.DAY_IN_WEEK)) {
    System.out.println("occurs monday");
}
rule.getInt(rule.DAY_IN_WEEK)

value is also same as monday value. 值也与星期一值相同。

Now the issue is, if the events is occuring on two/three or more number of days then 现在的问题是,如果事件发生在两天/三天或更多天

rule.getInt(rule.DAY_IN_WEEK)

returns me a sum of all days selected. 返回我选择的所有日期的总和。

EXAMPLE: if the days are :wed,sat then i get the result as 9216 ,sum of wed+sat ,from this i am not getting to know which are the days the event occurs. 例子:如果天数是:结婚,那么我得到结果为9216,结婚和坐的总和,从此我不知道事件发生的日期。

How can i do a permutation/combination of these numbers and get the exact result for 'n' number of days selected. 如何对这些数字进行排列/组合,并获得所选“n”天的确切结果。

I assume the days are just bit flags in a number so you might change your check: 我假设天数只是一个数字中的位标志,所以你可以改变你的支票:

if ( (rule.getInt(rule.DAY_IN_WEEK) & rule.MONDAY) != 0 ) {
   System.out.println("occurs monday");
}

Use binary and operator like this: 像这样使用二进制和运算符:

int day = rule.getInt(rule.DAY_IN_WEEK)
if(day & rule.MONDAY != 0) {
 System.out.println("occurs monday");
}
if(day & rule.WEDNESDAY != 0) {
 System.out.println("occurs wednesday");
} /* and so on */

Notice that: 请注意:

0000 0100 0000 0000 = 1024 0000 0100 0000 0000 = 1024

0000 1000 0000 0000 = 2048 0000 1000 0000 0000 = 2048

. . . check also bit mask 检查位掩码

To know which days of the week the event occurs, you need to do something like this: 要知道事件发生的一周中的哪几天,您需要执行以下操作:

boolean occursOnMonday = (rule.getInt(rule.DAY_IN_WEEK) & rule.MONDAY) != 0;

where & is the bitwise AND operator. where&是按位AND运算符。 Why is this so? 为什么会这样?

Wednesday is 8192, which in binary it is 10000000000000 (2 times 13) 周三是8192,二进制是10000000000000(2倍13)

Saturday is 1024, which in binary it is 00010000000000 (2 times 9) 星期六是1024,二进制是00010000000000(2倍9)

So an event that happens on Wed and Sat is 9216, which is 10010000000000. 因此,周三和周六发生的事件是9216,即10010000000000。

Then using bit operations you are able to know what bits are in 1 and what bits are in 0, and with that you know what days the event happens. 然后使用位操作,您可以知道1中的哪些位以及0中的位数,并且知道事件发生的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM