IOS4 note 二
bitwise operators
C also provides bitwise operators (K&R 2.9), such as bitwise-and (&) and bitwise-or
(|); they operate on the individual binary bits that constitute integers. Of these, the one
you are most likely to need is bitwise-or, because the Cocoa API often uses bits as
switches when multiple options are to be specified simultaneously. For example, there
are various ways in which a UIView can be resized automatically as its superview is
resized, and you’re supposed to provide one or more of these when setting a UIView’s
autoresizingMask property. The autoresizing options are listed in the documentation
as follows:
enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;
The << symbol is the left shift operator; the right operand says how many bits to shift
the left operand. So pretend that an NSUInteger is 8 bits (it isn’t, but let’s keep things
simple and short). Then this enum means that the following name–value pairs are
defined (using binary notation for the values):
UIViewAutoresizingNone
00000000
UIViewAutoresizingFlexibleLeftMargin
00000001
UIViewAutoresizingFlexibleWidth
00000010
UIViewAutoresizingFlexibleRightMargin
00000100
UIViewAutoresizingFlexibleTopMargin
00001000
and so on. The reason for this bit-based representation is that these values can be
combined into a single value (a bitmask) that you pass to set the autoresizingMask. All
Cocoa has to do in order to understand your intentions is to look to see which bits in
the value that you pass are set to 1. So, for example, 00001010 would mean that UIView-
AutoresizingFlexibleTopMargin and UIViewAutoresizingFlexibleWidth are true (and
that the others, by implication, are all false).
The question is how to form the value 00001010 in order to pass it. You could just do
the math, figure out that binary 00001010 is decimal 10, and set the autoresizingMask
property to 10, but that’s not what you’re supposed to do, and it’s not a very good idea,
because it’s error-prone and makes your code incomprehensible. Instead, use the
bitwise-or operator to combine the desired options:
myView.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
This notation works because the bitwise-or operator combines its operands by setting
in the result any bits that are set in either of the operands, so 00001000 | 00000010 is
00001010, which is just the value we’re trying to convey.
Bitwise Operation
<< shift left
>> shift right
& and
| or
^ xor
~ not
exp.
5 << 1 = 10 // 00101 shift left is 01010
5 >> 1 = 2 // 00101 shift right is 00010
5 << 1 means 5 * 2
5 >> 1 means 5 / 2
& AND
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
| OR
0 | 0 = 0
1 | 0 = 1
0 | 1 = 1
1 | 1 = 1
^ XOR
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
~ NOT
~ 0 = 1
~ 1 = 0