Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [82]

By Root 510 0
for common patterns like dates or URLs. The patterns an instance will look for is determined by the bitwise-OR result of a set of integer constants.

NSDataDetector.h defines these constants: NSTextCheckingTypeDate, NSTextCheckingTypeAddress, NSTextCheckingTypeLink, NSTextCheckingTypePhoneNumber, and NSTextCheckingTypeTransitInformation. When you create an instance of NSDataDetector, you tell it what to search for. For example, if you wanted it to search for phone numbers and dates, you would do this:

NSError *e;

NSDataDetector *d = [NSDataDetector dataDetectorWithTypes:

NSTextCheckingTypePhoneNumber|NSTextCheckingTypeDate

error:&e];

Notice the bitwise-OR operator. You’ll see this pattern a lot in Cocoa and iOS programming, and now you’ll know what’s going on behind the scenes.

Bitwise-AND


You can also bitwise-AND two bytes together to create a third. In this case, a bit on the third byte is 1 if the corresponding bits in the first two bytes are both 1.

Figure 33.4 Two bytes bitwise-ANDed together

This is done with the & operator. Add the following lines to main.c:

#include

int main (int argc, const char * argv[])

{

unsigned char a = 0x3c;

unsigned char b = 0xa9;

unsigned char c = a | b;

printf("Hex: %x | %x = %x\n", a, b, c);

printf("Decimal: %d | %d = %d\n", a, b, c);

unsigned char d = a & b;

printf("Hex: %x & %x = %x\n", a, b, d);

printf("Decimal: %d & %d = %d\n", a, b, d);

return 0;

}

When you run it, you will see the two bytes bitwise-ANDed together:

Hex: 3c & a9 = 28

Decimal: 60 & 169 = 40

In Objective-C, we use bitwise-AND to see if a certain bit, or flag, is on. For example, if you were handed an instance of NSDataDetector, you could check if it was set to look for phone numbers like this:

if ([currentDetector checkingTypes] & NSTextCheckingTypePhoneNumber) {

NSLog(@"This one is looking for phone numbers");

}

The checkingTypes method returns an integer that is the bitwise-OR result of all the flags this instance of NSDataDetector has on. You bitwise-AND this integer with a particular NSTextCheckingType constant and check the result. If the bit that is on in NSTextCheckingTypePhoneNumber is not on in the data detector’s setting, then the result of bitwise-ANDing them will be all zeroes. Otherwise, you’ll get a non-zero result, and you’ll know that this data detector does look for phone numbers.

Note that when we use bits this way, we don’t care what the integers in these cases equate to numerically. We use the bit placement within the integer to represent something other than a certain power of 2.

Other bitwise operators


For completeness, here are the other bitwise operators. These are less commonly used in Objective-C but good to know.

Exclusive OR

You can exclusive-or (XOR) two bytes together to create a third. A bit in the third byte is 1 if exactly one of the corresponding bits in the input bytes is 1.

Figure 33.5 Two bytes bitwise-XORed together

This is done with the ^ operator. Add to main.c:

unsigned char e = a ^ b;

printf("Hex: %x ^ %x = %x\n", a, b, e);

printf("Decimal: %d ^ %d = %d\n", a, b, e);

return 0;

}

When you run it you will see:

Hex: 3c ^ a9 = 95

Decimal: 60 ^ 169 = 149

This operator sometimes causes beginners some confusion. In most spreadsheet programs, the ^ operator is exponentiation: 2^3 means 23. In C, we use the pow() function for exponentiation:

double r = pow(2.0, 3.0); // Calculate 2 raised to the third power

Complement

If you have a byte, the complement is the byte that is the exact opposite: each 0 becomes a 1 and each 0 becomes a 1.

Figure 33.6 The complement

This is done with the ~ operator. Add a few lines to main.c:

unsigned char f = ~b;

printf("Hex: The complement of %x is %x\n", b, f);

printf("Decimal: The complement of %d is %d\n", b, f);

return 0;

}

You should see:

Hex: The complement of a9 is 56

Decimal: The complement of 169 is 86

Left-shift

If you left-shift the bits, you take each bit and move it toward the most

Return Main Page Previous Page Next Page

®Online Book Reader