怎样通过Objective-C的枚举学习iOS中位操作.md
创始人
2024-12-19 21:07:39
0次

位操作

位操作是对二进制数逐位进行运算或移位。它共包含两种操作:位运算和移位。下面就详细的了解一下这两种操作。

在此只讨论iOS中的所有位操作的运算符,别的语言的相同含义的操作符号可能不同

位运算符(以下操作符皆同Objective-C)

位运算符一种包含下面几种:

~(取反,一元操作符):它会对目标数字的二进制每位进行取反

let initialBits: UInt8 = 0b00001111let invertedBits = ~initialBits // equals 11110000

|(按位或):它会对两个目标数字的相同位置数字进行或运算,规则:0和0为0;0和1为1;1和1为1

let targetNum = 5 // 101let targetNum2 = 6 // 110print(targetNum | targetNum2) //print 7//targetNum: 101//targetNum2: 110//result:  111 (十进制 7)

&(按位与):它会对两个目标数字的相同位置数字进行与运算,规则:0和0为0;0和1为0;1和1为1

let targetNum = 5 // 101let targetNum2 = 6 // 110print(targetNum & targetNum2) //print 4//targetNum: 101//targetNum2: 110//result:  100 (十进制 4)

^(异或):它会对两个目标数字的相同位置数字进行异或运算,如果不同则该位为1,否则该位为0。规则:如0和0为0;0和1为1;1和1为0

let targetNum = 5 // 101let targetNum2 = 6 // 110print(targetNum ^ targetNum2) //print 3//targetNum: 101//targetNum2: 110//result:  011 (十进制 3)

移位

>>(右移):它会对目标数字按位右移x位

let targetNum = 5 // 101print(targetNum >> 2) //print 1//targetNum: 101//右移2位//result:  1 (十进制 1)

<<(左移):它会对目标数字按位左移x位(右边补0)

let targetNum = 5 // 101print(targetNum << 2) //print 20//targetNum: 101//左移2位//result:  10100 (十进制 20)

枚举中的位操作

通过上文我们了解了位操作的具体计算方式,接下来看一下在枚举中的具体应用。

枚举中的应用

定义枚举

OC

typedef NS_OPTIONS(NSInteger, CellExLineType) { CellExLineTypeTopLong  = 0, CellExLineTypeTopNone  = 1 << 0, //十进制 1 CellExLineTypeBottomLong = 1 << 1, //十进制 2 CellExLineTypeBottomNone = 1 << 2, //十进制 4};

Swift

struct CellExLineType: OptionSet { let rawValue: Int static let topLong = CellExLineType(rawValue: 0) static let topNone = CellExLineType(rawValue: 1 << 0) static let bottomLong = CellExLineType(rawValue: 1 << 1) static let bottomNone = CellExLineType(rawValue: 1 << 2)}

位操作在枚举中的作用

~(取反):用来剔除某个值

//OCself.lineType = CellExLineTypeTopNone;self.lineType |= CellExLineTypeBottomNone;self.lineType = self.lineType & ~CellExLineTypeTopNone; //self.lineTye 只包含CellExLineTypeBottomNone//Swiftvar lineType: CellExLineType = [.topNone, .bottomNone]lineType.remove(.topNone)

|(按位或):用来添加某个值

//OCself.lineType = CellExLineTypeTopNone; //self.lineType 包含CellExLineTypeTopNoneself.lineType = self.lineType | CellExLineTypeBottomNone; //self.lineType 包含CellExLineTypeTopNone和CellExLineTypeBottomNone//Swiftvar lineType: CellExLineType = [.bottomNone]lineType.insert(.topNone)

&(按位与):用来检查是否包含某个值

//OCif ((self.lineType & CellExLineTypeTopNone) == CellExLineTypeTopNone) { NSLog(@"包含CellExLineTypeTopNone");}//Swiftvar lineType: CellExLineType = [.topNone, .bottomNone]if lineType.contains(.bottomNone) { print("包含bottomNone")}

^(异或):用来置反某个值(如果包含则剔除,如果不包含则添加)

//OCself.lineType = CellExLineTypeTopNone | CellExLineTypeBottomNone; //self.lineType 包含CellExLineTypeTopNone和CellExLineTypeBottomNoneself.lineType = self.lineType ^ CellExLineTypeTopNone; //self.lineTye 只包含CellExLineTypeBottomNoneself.lineType = self.lineType ^ CellExLineTypeTopNone; //self.lineType 包含CellExLineTypeTopNone和CellExLineTypeBottomNone//Swiftvar lineType: CellExLineType = [.topNone, .bottomNone]if lineType.contains(.topNone) { lineType.remove(.topNone)} else { lineType.insert(.topNone)}

在枚举中使用位操作我们可以方便的给一个属性值赋值多个值,比如下面的代码给lineType赋值了CellExLineTypeTopNone和CellExLineTypeBottomNone属性。这样我们就可以在lineType的set方法里面处理CellExLineTypeTopNone和CellExLineTypeBottomNone的情况。

//OC- (void)setLineType:(CellExLineType)lineType { _lineType = lineType; if (lineType & CellExLineTypeTopNone) {  NSLog(@"top none"); } if (lineType & CellExLineTypeBottomNone) {  NSLog(@"bottom none"); }}//Swiftvar lineType: CellExLineType = [.topNone, .bottomNone]if lineType.contains(.topNone) { lineType.remove(.topNone)}if lineType.contains(.bottomNone) {}

在系统中的许多Enum也是这么使用的,如UIViewAutoresizing、UIViewAnimationOptions等。

为什么要在枚举中使用位操作符?

在许多古老的微处理器上,位运算比加减运算略快,通常位运算比乘除法运算要快很多。在现代架构中,情况并非如此:位运算的运算速度通常与加法运算相同(仍然快于乘法运算)可以给一个属性同时设置多个值

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...