Alex Rivera | Logout

How do those bitmasks actually work?

Asked 2010-03-31T17:31:57.273
26

For example, this method from NSCalendar takes a bitmask:

- (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSUInteger)opts

So options can be like:

NSUInteger options = kCFCalendarUnitYear;

or like:

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?

Edit
Report

1 Answer

0

I've found Calculator.app to be helpful in visualizing bit masks. (Just choose View > Programmer, and then click the button to Show Binary). (You can click on any of the 0s or 1s in the binary table to switch those bits on or off; or, enter numbers in decimal or hex (use the 8 | 10 | 16 NSSegmentedControl to switch between different representations)).

answered 2010-04-01T02:15:33.680

Your Answer