美文网首页
位运算的使用

位运算的使用

作者: 冷武橘 | 来源:发表于2020-04-20 09:31 被阅读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或有红色提醒时作出回调

相关文章

  • 位运算的使用

    已知int a= 2; int b= 12;a的二进制形式是0010,b的二进制形式是1110; 一、位与运...

  • 位运算的使用

    已知int a= 2; int b= 12;a的二进制形式是0010,b的二进制形式是1110; 一、位与运...

  • 使用位运算实现加法运算

    简书上的文章已经不再维护,有兴趣阅读其他文章,或一起交流的朋友,请移步 我的博客:punmy.cn 原文 位操作实...

  • 位运算使用总结

    今天总结一下,二进制在开发过程中的使用 很多人都觉得 二进制和十进制转换是件麻烦的事,所以很少去用二进制,其实二进...

  • HashMap - 为什么数组扩容是二倍

    1. 增加运算效率 扩容时使用位运算<<,计算除余时使用(n-1)&hash,这些位运算都可以增加效率 2. 减少...

  • PHP 位运算的使用

    位运算符 如果你正准备看下去,你应该先搞懂各个位运算符的作用。 以下是官网的一个介绍。 例子名称结果 $a & $...

  • 位运算的基本使用

    看源码时经常能看到一些位运算的操作,这里列举几种常见的用法。 乘除 乘2^n 除2^n 取模 设置具体bit位的数...

  • Java位运算的基础及使用(意义)

    Java位运算的基础及使用

  • Python基础之位运算符(含原码反码补码的通俗解释)

    目录 1 二进制 2 原码、反码、补码 3 位运算符 4 位运算符使用技巧 上回学习运算符时,漏了位运算符,因为位...

  • 位运算

    参考:位运算技巧 位运算的使用 1.and运算and运算通常用于二进制取位操作,例如一个数and1的结果就是取二进...

网友评论

      本文标题:位运算的使用

      本文链接:https://www.haomeiwen.com/subject/ryllihtx.html