

Objective-C Bitmask Enumeration Essentials - pumpmybicep
http://www.pumpmybicep.com/2014/05/22/objective-c-bitmask-enumeration-essentials

======
tonysuper
Isn't this applicable to any kind of bitmask, and not just Obj-C?

I suppose that Cocoa uses bitmasks fairly often, but this article could have
been titled "Working With Bitmasks in C" and it would have been just as
accurate.

------
anon1385
>Use the bitwise AND operator & to check for a selected bitmask enumeration
value.

You should take care if you store result of this in a BOOL (or pass it to a
method or function that takes a BOOL). BOOL is only char big and plenty of
common Cocoa bitmasks don't have any bits set in that range. For example
NSEventMaskEndGesture is (1<<20).

    
    
      NSEventMask mask = NSEventMaskEndGesture;
      BOOL end = mask & NSEventMaskEndGesture;
      if (!end) NSLog(@"Oops");

~~~
MaxGabriel
Is this the correct approach?

    
    
        BOOL end = mask & NSEventMaskEndGesture ? YES : NO;

~~~
plorkyeran
(mask & NSEventMaskEndGesture) == NSEventMaskEndGesture is the most correct,
as it handles the case where NSEventMaskEndGesture has multiple bits set.

------
al_gore
This is just plain old C (which, yes, Objective-C is a superset of).

