<< - bit shift operator. So 1 << 2 says it translates the bit into two spaces.
Example:
In binary expression, the number 1 is:
0001
1 << 2 means the shift of all bits to the left 2 spaces, which leads to this value:
0100
or 4 .
Thus, the values ββof each ENUM in your example are: 1, 2, 4, 8, 16, etc. They could precisely set each enumeration to these values. But since they use this enumeration for multiple values, binary values ββmake it more understandable:
0001 0010 0100 1000
so they write using bit shifts.
so if I OR ( | ) two of these values ββtogether, for example FlexibleLeftMargin ( 0001 ) and FlexibleWidth ( 0010 ), I get the following value:
0011
Therefore, they use each bit as a flag, so they know that you have several values ββset.
Now you can use the AND & operator to find out if a specific value is set.
0010 & 0011 = 0010
So you can do this to check if you have one of the enumerations:
myenum = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin); if((myenum & UIViewAutoresizingFlexibleLeftMargin) == UIViewAutoresizingFlexibleLeftMargin) {
Hope this makes sense. For a more detailed explanation of bitwise operations, read this: Wikipedia ~ Bit Operators or search around for β bit operators β
Joel Nov 14 '10 at 4:26 2010-11-14 04:26
source share