How to dynamically compose a bitmask?

Posted by mystify on Stack Overflow See other posts from Stack Overflow or by mystify
Published on 2010-03-31T17:36:16Z Indexed on 2010/03/31 17:43 UTC
Read the original article Hit count: 500

Filed under:
|

Lets say I have to provide an value as bitmask.

NSUInteger options = kFoo | kBar | kFooBar;

and lets say that bitmask is really huge and may have 100 options. But which options I have, depends on a lot of situations. How could I dynamically compose such a bitmask?

Is this valid?

NSUInteger options;

if (foo) {
    options = options | kFoo;
}

if (bar) {
    options = options | kBar;
}

if (fooBar) {
    options = options | kFooBar;
}

(despite the fact that this would probably crash when doing that | bitmask operator thing to "nothing".

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c