Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
11
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать

Chapter 33

Bitwise Operations

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

$ hexdump myfile.txt

 

 

 

 

 

 

 

 

 

 

 

 

0000000

3c

3f

78

6d

6c

20

76

65

72

73

69

6f

6e

3d

22

31

0000010

2e

30

22

3f

3e

0a

3c

62

6f

6f

6b

20

78

6d

6c

6e

0000020

73

3d

22

68

74

74

70

3a

2f

2f

64

6f

63

62

6f

6f

0000030

6b

2e

6f

72

67

2f

6e

73

2f

64

6f

63

62

6f

6f

6b

0000040

22

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0000041

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The first column is the offset (in hex) from the beginning of the file of the byte listed in the second column. Each two digit number represents one byte.

Bitwise-OR

If you have two bytes, you can bitwise-OR them together to create a third byte. A bit on the third byte will be 1 if at least one of the corresponding bits in the first two bytes is 1.

Figure 33.3 Two bytes bitwise-ORed together

This is done with the | operator. To try your hand at manipulating bits, create a new project: a C Command Line Tool (not Foundation) named bitwize.

Edit main.c:

#include <stdio.h>

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);

return 0;

}

When you run this program, you will see the two bytes bitwise-ORed together:

Hex: 3c | a9 = bd

Decimal: 60 | 169 = 189

What’s this good for? In Objective-C, we often use an integer to specify a certain setting. An integer is always a sequence of bits, and each bit is used to represent one aspect of the setting that can be turned

240

Bitwise-AND

on or off. We create this integer (also known as a bit mask) by picking and choosing from a set of constants. These constants are integers, too, and each constant specifies a single aspect of the setting by having only one of its bits turned on. You can bitwise-OR together the constants that represent the particular aspects you want. The result is the exact setting you’re looking for.

Let’s look at an example. iOS comes with a class called NSDataDetector. Instances of NSDataDetector go through text and look 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 <stdio.h>

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);

241

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]