Friday, September 28, 2012

Bitwise operator OR and AND - in objective C - Exclusive OR - Complements - Shifting and Enum


Bitwise operator OR and AND - in objective C - Exclusive OR - Complements - Shifting and Enum


  • | is the bitwise-OR operator in C. 


You will see the bitwise-OR operator a lot in Cocoa and iOS programming.
You can use bitwise-OR together the constants that represent the particular aspects you want.


NSError *e;
NSDataDetector *d = [NSDataDetector dataDetectorWithTypes:
                                             NSTextCheckingTypePhoneNumber | NSTextCheckingTypeDate
                                             error:&e];


  • In objective-C we use bitwise-AND (&) to see if a certain bit, or flag is on

if ([currentDetector checkingTypes] & NSTextCheckingTypePhoneNumber){
    NSLog (@"This one is looking for phone numbers");
}



  • ^ exclusive OR (XOR) - two bytes together to create a third (beware is not exponentiation operator=pow())
  • complement is the byt that is the exact opposite of another (reverse all the 0 to 1 and all the 1 becomes 0).
  • A<<2 .="." b="b" nbsp="nbsp"><< Left-shift
. Every time you left -shift a number one place, you double tis value.
  • A>>1. >>Right-shift. Every time you right-shift a number one place, you half its value (if it is odd round down).

  • An elegant way to declare the bit-masks with enum
    enum {
     UIDataDetectorTypePhoneNumber = 1 << 0,
     UIDataDetectorTypeLink                = 1 << 1,
     UIDataDetectorTyprAddress           = 1 << 2,
     UIDataDetectorTypeNone               =0,
     UIDataDetectorTypeAll                   = NSUIntegerMax
    };




    examples from The big Nerd Ranch Guide - Objective C 

    No comments:

    Post a Comment