那些位掩码如何工作? [英] How do those bitmasks actually work?
本文介绍了那些位掩码如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,NSCalendar的这个方法需要一个位掩码:
For example, this method from NSCalendar takes a bitmask:
- (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSUInteger)opts
p>
So options can be like:
NSUInteger options = kCFCalendarUnitYear;
或类似:
NSUInteger options = kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay;
我没有得到的是,这是怎么做的?我的意思是:他们如何拉出合并到选项
的那些值?
What I don't get is, how is this actually done? I mean: How can they pull out those values which are merged into options
? If I wanted to program something like this, that can take a bitmask, how would that look?
推荐答案
要做到这一点,你需要想要将AND测试的值与掩码进行比较,然后查看ANDing的结果是否等于掩码本身:
To do this, you want to bitwise AND the value you're testing against the mask, then see if the result of the ANDing equals the mask itself:
if ((options & kCFCalendarUnitYear) == kCFCalendarUnitYear) {
// do whatever
}
这篇关于那些位掩码如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文