0

I was looking at PSPDFkit sample code and saw this:

NSDictionary *options = @{kPSPDFProcessorAnnotationTypes :
                               @(PSPDFAnnotationTypeNone & ~PSPDFAnnotationTypeLink)
                         };

The constants PSPDFAnnotationTypeNone and PSPDFAnnotationTypeLink are defined below:

// Available keys for options. kPSPDFProcessorAnnotationDict in
// form of pageIndex -> annotations.
// ..
extern NSString *const kPSPDFProcessorAnnotationTypes;

// Annotations defined after the PDF standard.
typedef NS_OPTIONS(NSUInteger, PSPDFAnnotationType) {
    PSPDFAnnotationTypeNone      = 0,
    PSPDFAnnotationTypeLink      = 1 << 1,  // Links and multimedia extensions
    PSPDFAnnotationTypeHighlight = 1 << 2,  // (Highlight, Underline, StrikeOut) - 
    PSPDFAnnotationTypeText      = 1 << 3,  // FreeText
    PSPDFAnnotationTypeInk       = 1 << 4,
    PSPDFAnnotationTypeShape     = 1 << 5,  // Square, Circle
    PSPDFAnnotationTypeLine      = 1 << 6,
    PSPDFAnnotationTypeNote      = 1 << 7,
    PSPDFAnnotationTypeStamp     = 1 << 8,
    PSPDFAnnotationTypeRichMedia = 1 << 10, // Embedded PDF videos
    PSPDFAnnotationTypeScreen    = 1 << 11, // Embedded PDF videos
    PSPDFAnnotationTypeUndefined = 1 << 31, // any annotation whose type not recognized
    PSPDFAnnotationTypeAll       = UINT_MAX
};

I understand that ~ is the bitwise not operator and & the bitwise and operator, but what is the purpose of their application in this code?

NSDictionary *options = @{kPSPDFProcessorAnnotationTypes :
                               @(PSPDFAnnotationTypeNone & ~PSPDFAnnotationTypeLink)
                         };

Based on comments below, the above could have been written simply as

NSDictionary *options = @{kPSPDFProcessorAnnotationTypes :@(PSPDFAnnotationTypeNone)};

Since it is the same as (0 & ~2) => 0. What's the point of adding the & ~PSPDFAnnotationTypeLink part?

jscs
  • 63,694
  • 13
  • 151
  • 195
abbood
  • 23,101
  • 16
  • 132
  • 246
  • In the context of this code it seems to be an obfuscated way of getting 0: `(0 & ~2)` => 0 – Carl Veazey Apr 17 '13 at 06:38
  • so what value does writing the extra `& ~2` add? why not just put `0`? – abbood Apr 17 '13 at 06:39
  • That I don't know... would vote to reopen were question to focus on why. – Carl Veazey Apr 17 '13 at 06:47
  • @CarlVeazey cool.. updated the question and voted for it to re-open – abbood Apr 17 '13 at 06:58
  • Why is the `enum` skipping 1<<0? Or 1<<9? And why is it using the wrong type for the `All` option? – jscs Apr 17 '13 at 07:08
  • John: The 1<<0 is used in more recent versions, 1<<9 has legacy reasons, and what is wrong with UINT_MAX? (Since the enum type is NSUInteger?) – steipete Apr 17 '13 at 21:42
  • @steipete: `NSUInteger`'s underlying type is `unsigned long`, and the maximum is `NSUIntegerMax`. An `NSUInteger` will be longer than an `unsigned int` on the desktop or if iOS ever gets 64-bit processors. – jscs Apr 18 '13 at 18:46
  • Josh - thanks! Just fixed that in my code. – steipete Apr 18 '13 at 22:01
  • @steipete: Oh! I didn't realize this was your code; I thought it was the question poster's. Well, glad I could help! – jscs Apr 19 '13 at 20:52

3 Answers3

2

"~" is the bitwise not-operator.

As "&" the bitwise and.

These are usually used for bitmask (like in your example) or other binary operations (as the name lets suggest). More info on wiki - Operators in C and C++.

They are in no relationship to literals.

yinkou
  • 5,756
  • 2
  • 24
  • 40
0

First of all, I don't know obj-c, only C, but I guess the '&' is 'bitwise AND' and the '~' is bitwise NOT.

0

It's the bitwise NOT operator (same as many C-based languages), which inverts all bits in the underlying value.

So, for example, the eight bit value 0x57 (binary 0101 0111) becomes 1010 1000 or 0xa8.

See here for a more complete description of the various bitwise operators.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953