在iOS中按位OR

时间:2011-07-02 17:46:49

标签: c ios

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;

这个表达的确切含义是什么:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse

UIViewAnimationOptionRepeat的值等于8(在bin 1000中),UIViewAnimationOptionAutoreverse等于16(在bin 10000中)。所以表达式UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse应该生成我认为16(bin 10000) - &gt; UIViewAnimationOptionReverse。

4 个答案:

答案 0 :(得分:3)

操作|由真值表

定义
   | 0 | 1  
---+---+---
 0 | 0 | 1
 1 | 1 | 1

x | y == 0 x == 0y == 0|运算符同时处理机器字的所有位。所以

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

答案 1 :(得分:1)

UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse被称为“面具”。

如果您有UIViewAnimationOptions类型的变量,请说:

 UIViewAnimationOptions a;

你可以像这样对它应用蒙版:

 bool b = a && (UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse)

确定a“是否包含”任一标志。如果

a == 0x0000001;

然后

b == false;

如果

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

然后

 b == true;

因此,您实际上并不感兴趣UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse评估的内容,而只是在逻辑上将该类型的值转换为您要检查的标志的结果。

答案 2 :(得分:0)

这些位是或者是

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

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

11000二进制是16 + 8 = 24 - 一个整数,第三和第四位设置(从0开始计数)。

答案 3 :(得分:0)

UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse

相当于

01000
10000 |

结果是

11000

这不是你假设的10000