Live data from Hacker News

Objective-C Bitmask Enumeration Essentials

pumpmybicep.com

1–9 of 9 posts

Re: Objective-C Bitmask Enumeration Essentials

#2
>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

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

Re: Objective-C Bitmask Enumeration Essentials

#3
post #2

>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 NSEventMask mask = NSEventMaskEndGesture; BOOL end = mask & NSEventMaskEndGesture; if…

Is this the correct approach?

    BOOL end = mask & NSEventMaskEndGesture ? YES : NO;

Re: Objective-C Bitmask Enumeration Essentials

#4
post #2

>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 NSEventMask mask = NSEventMaskEndGesture; BOOL end = mask & NSEventMaskEndGesture; if…

Is this the correct approach? BOOL end = mask & NSEventMaskEndGesture ? YES : NO;

or just != 0

Re: Objective-C Bitmask Enumeration Essentials

#6
post #2

>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 NSEventMask mask = NSEventMaskEndGesture; BOOL end = mask & NSEventMaskEndGesture; if…

Is this the correct approach? BOOL end = mask & NSEventMaskEndGesture ? YES : NO;

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

Re: Objective-C Bitmask Enumeration Essentials

#8
post #2

>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 NSEventMask mask = NSEventMaskEndGesture; BOOL end = mask & NSEventMaskEndGesture; if…

Is this the correct approach? BOOL end = mask & NSEventMaskEndGesture ? YES : NO;

I think, not!!

   BOOL end = !! (mask & NSEventMaskEndGesture);
Like a little pair of surprised eyes.

Also works for pointers:

    BOOL hasError = !! errorObj;