开发者

bitwise OR in iOS

开发者 https://www.devze.com 2023-03-17 08:22 出处:网络
enum { UIViewAnimationOptionLayoutSubviews= 1 <<0, UIViewAnimationOptionAllowUserInteraction= 1 <<1,
enum {
   UIViewAnimationOptionLayoutSubviews            = 1 <<  0,
   UIViewAnimationOptionAllowUserInteraction      = 1 <<  1,
   UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2,
   UIViewAnimationOptionRepeat                    = 1 <<  3,
   UIViewAnimationOptionAutoreverse               = 1 <<  4,
   UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5,
   UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6,
   UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7,
   UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8,

   UIViewAnimationOptionCurveEaseInOut            = 0 << 16,
   UIViewAnimationOptionCurveEaseIn               = 1 << 16,
   UIViewAnimationOptionCurveEaseOut         开发者_如何转开发     = 2 << 16,
   UIViewAnimationOptionCurveLinear               = 3 << 16,

   UIViewAnimationOptionTransitionNone            = 0 << 20,
   UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
   UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
   UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
   UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
};
typedef NSUInteger UIViewAnimationOptions;

What exactly means this expression: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse.

Value of UIViewAnimationOptionRepeat is equal to 8(in bin 1000), UIViewAnimationOptionAutoreverse is equal to 16(in bin 10000). So expression UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse should generate as I think 16(bin 10000) -> UIViewAnimationOptionReverse.


The operation | is defined by the truth table

   | 0 | 1  
---+---+---
 0 | 0 | 1
 1 | 1 | 1

that is, x | y == 0 only if both x == 0 and y == 0. The | operator works on all bits of a machine word at the same time. So

  001000   (8)
| 010000  (16)
  ------------
  011000  (24)


UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse is known as a "mask".

If you have a variable of type UIViewAnimationOptions, say:

 UIViewAnimationOptions a;

you can apply the mask to it like this:

 bool b = a && (UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse)

to determine if a "contains" either flags. If

a == 0x0000001;

then

b == false;

if

a == 0x0101001;  //-- completely arbitrary mask

then

 b == true;

So you are not actually interested in what UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse evaluates to, but only in the result of logically and-ing a value of that type to the flags you are interested in checking.


The bits are or'd:

UIViewAnimationOptionRepeat      = 1 << 3 = 8  = 01000 in binary
UIViewAnimationOptionAutoreverse = 1 << 4 = 16 = 10000 in binary

   01000
OR 10000
--------
   11000

11000 in binary is 16 + 8 = 24 - an integer with third and fourth bits set (counting from 0).


UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse

is equivalent to

01000
10000 |

the result of which is

11000

which is not 10000 as you had assumed.

0

精彩评论

暂无评论...
验证码 换一张
取 消