Definition of bit masks in objective-c -
i learning objective-c , can't understand bitmasks, can me understand please? , don't know function of operator <<
is.
objective c existing of c, , uses same bitwise operators. lets take uiremotenotificationtype
example:
uiremotenotificationtypenone = 0, uiremotenotificationtypebadge = 1 << 0, uiremotenotificationtypesound = 1 << 1, uiremotenotificationtypealert = 1 << 2, uiremotenotificationtypenewsstandcontentavailability = 1 << 3,
the <<
shift left operator , function obvious once @ binary form:
1 << 0 = 1 (decimal) = 0000001 (binary) 1 << 1 = 2 (decimal) = 0000010 (binary) 1 << 2 = 4 (decimal) = 0000100 (binary) 1 << 3 = 8 (decimal) = 0001000 (binary)
it shifts specific pattern (the left operand) left, 'length' of shift determined right operand. works other numbers 1; 3 << 2 = 12
because 0000011 (binary) shifted 2 places 0001100. translated normal mathematics, a << b = * 2^b
.
the specific use of pattern is easy check if option set. suppose want application send notifications badges , alerts. pass value uiremotenotificationtypebadge | uiremotenotificationtypealert
api, is
uiremotenotificationtypebadge = 0000001 uiremotenotificationtypealert = 0000100 total = 0000101 |
(the | bitwise or operator; every bit, result 1 if 1 or both of corresponding bit of operands 1). api can check if badge property present & operator:
total = 0000101 uiremotenotificationtypebadge = 0000001 result = 0000001 &
(the & bitwise , operator; every bit, result 1 if both of corresponding bit of operands 1). result non-zero, badge property present. let's same sound property:
total = 0000101 uiremotenotificationtypesound = 0000010 result = 0000000 &
the result zero, badge property not present.
Comments
Post a Comment