位运算的使用

2020-04-20  本文已影响0人  冷武橘

已知int a= 2; int b= 12;a的二进制形式是0010,b的二进制形式是1110;

一、位与运算

     a & b:只有对应的两个二进位均为1时,结果位才为1,否则为0。
     0    0   1   0
     位与运算
     1    1   1   0
     -----------------------
     0    0   1   0

二、位或运算

  a | b : a,b对应的二个二进位有一个为1时,结果位就为1,否则为0
     0    0   1   0
     位或运算
     1    1   1   0
     -----------------------
     1    1   1   0

三、位左移运算

整数a(0010) << 正数n:整数a的二进位左移n位

     a << 1 -> 0 1 0 0 
     a << 2 -> 1 0 0 0
     a << 3 -> 0 0 0 1 0 0 0 0

四、位右移运算

  整数a(0010) >> 整数n:整数a的二进位右移n位

  a >> 1 -> 0 0 0 1

五、位运算在枚举中的使用

//1、简单应用举例
typedef NS_OPTIONS(NSUInteger, State) {
    StateNormal       = 0,         //0000  正常状态下
    StateHighlighted  = 1 << 0,    //0001  高亮状态
    StateDisabled     = 1 << 1,    //0010  不能够点击状态
    StateSelected     = 1 << 2,    //0100。 选择状态
};

-(void)statestatus:(NSUInteger)allstated{
    if (allstated==0) {
        NSLog(@"正常情况下");
        return;
    }
    if (allstated&StateHighlighted) { //0110&0001=0000 !=StateHighlighted
        NSLog(@"我可以高亮");
    }
    if (allstated&StateDisabled) {//0110&0010=0010 == StateDisabled
        NSLog(@"我可以交互");
    }
    if (allstated&StateSelected) {//0110&0100=0100 StateSelected
        NSLog(@"我可以选择");
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //如果你想让他在StateSelected或StateDisabled状态下可以执行的一些事情,你就可以像这样调用
    [self statestatus:StateSelected|StateDisabled];//0100|0010 = 0110,即是StateSelected或StateDisabled状态下可以执行的一些事情
    
}


//2、系统常见的含位运算的枚举
typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
    UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
    UNNotificationPresentationOptionBadge   = (1 << 0),
    UNNotificationPresentationOptionSound   = (1 << 1),
    UNNotificationPresentationOptionAlert   = (1 << 2),
}

    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal|UIControlStateHighlighted];
    //设置按钮文字的颜色在高亮或正常状态下都为红色
    
    completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound);
    //通知中心在有声音或有alter或有红色提醒时作出回调

上一篇下一篇

猜你喜欢

热点阅读