Graphics Programs Reference
In-Depth Information
Back in the code, notice the | operator in the argument of setAutoresizingMask: .
That is the bitwise-OR operator. Each autoresizing constant is equal to a power of two.
(You can find all the UIViewAutoresizing constants in the UIView class reference
page in the documentation.) For example, the flexible left margin constant is 1 (2 0 ), and
the flexible height constant is 16 (2 4 ). The property autoresizingMask is just an int
and, like all values on a computer, is represented in binary. Binary numbers are a string of
1s and 0s. Here are a few examples of numbers in base 10 (decimal; the way we think
about numbers) and base 2 (binary; the way a computer thinks about numbers):
1 10 = 00000001 2
2 10 = 00000010 2
16 10 = 00010000 2
27 10 = 00011011 2
34 10 = 00100010 2
In decimal representation, we have 10 different digits: 0 - 9 . When we count past 9, we
run out of symbols to represent the number, so we add a new digit column. A digit in the
second column is worth 10 times more than a digit in the first column; a digit in the third
column is worth 10 times more than the second column and so on. The same general idea
is true for binary numbers, except we only have two digits ( 0 and 1 ), so we must add a
new digit column each time we would normally use a 2. Because of this, each digit in bin-
ary is only worth two times more than the digit to the right of it. The rightmost digit is
multiplied by 1, the one to the left of that is multiplied by 2, then 4, 8, and so on.
When talking about binary numbers, we call each digit a bit . We can think of each bit as
an on-off switch, where 1 is “on” and 0 is “off.” When thinking in these terms, we can use
an int (which has space for at least 32 bits) as a set of on-off switches. Each position in
the number represents one switch - a value of 1 means true, 0 means false. Essentially, we
are shoving a ton of BOOL s into a single value. We call numbers used in this way bit-
masks , and that's why the autoresize settings of a view are called the autoresizing mask.
A number that only has one bit set to 1 (the rest are 0) is a power of two. Therefore, we
can use numbers that are powers of two to represent a single switch in a bitmask - each
autoresizing constant is a single switch. We can turn on a switch in a bitmask using the
bitwise-OR operation. This operation takes two numbers and produces a number where a
Search WWH ::




Custom Search