开篇
今天在修改项目的时候,看见enum中出现了<<
操作符(位操作),之前对这个一直都不了解。这次趁着项目比较清闲,抽出时间来全面了解一下位操作。
位操作
位操作是对二进制数逐位进行运算或移位。它共包含两种操作:位运算和移位。下面就详细的了解一下这两种操作。在此只讨论iOS中的所有位操作的运算符,别的语言的相同含义的操作符号可能不同
位运算符(以下操作符皆同Objective-C)
位运算符一种包含下面几种:
~
(取反,一元操作符):它会对目标数字的二进制每位进行取反12let initialBits: UInt8 = 0b00001111let invertedBits = ~initialBits // equals 11110000|
(按位或):它会对两个目标数字的相同位置数字进行或运算,规则:0和0为0;0和1为1;1和1为1
123456let 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
123456let 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
123456let targetNum = 5 // 101let targetNum2 = 6 // 110print(targetNum ^ targetNum2) //print 3//targetNum: 101//targetNum2: 110//result: 011 (十进制 3)
移位
>>
(右移):它会对目标数字按位右移x位12345let targetNum = 5 // 101print(targetNum >> 2) //print 1//targetNum: 101//右移2位//result: 1 (十进制 1)<<
(左移):它会对目标数字按位左移x位(右边补0)12345let targetNum = 5 // 101print(targetNum << 2) //print 20//targetNum: 101//左移2位//result: 10100 (十进制 20)
枚举中的位操作
通过上文我们了解了位操作的具体计算方式,接下来看一下在枚举中的具体应用。
枚举中的应用
定义枚举
OC
123456typedef NS_OPTIONS(NSInteger, CellExLineType) {CellExLineTypeTopLong = 0,CellExLineTypeTopNone = 1 << 0, //十进制 1CellExLineTypeBottomLong = 1 << 1, //十进制 2CellExLineTypeBottomNone = 1 << 2, //十进制 4};Swift
12345678struct CellExLineType: OptionSet {let rawValue: Intstatic 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)}
位操作在枚举中的作用
~(取反):用来剔除某个值
12345678//OCself.lineType = CellExLineTypeTopNone;self.lineType |= CellExLineTypeBottomNone;self.lineType = self.lineType & ~CellExLineTypeTopNone; //self.lineTye 只包含CellExLineTypeBottomNone//Swiftvar lineType: CellExLineType = [.topNone, .bottomNone]lineType.remove(.topNone)|(按位或):用来添加某个值
1234567//OCself.lineType = CellExLineTypeTopNone; //self.lineType 包含CellExLineTypeTopNoneself.lineType = self.lineType | CellExLineTypeBottomNone; //self.lineType 包含CellExLineTypeTopNone和CellExLineTypeBottomNone//Swiftvar lineType: CellExLineType = [.bottomNone]lineType.insert(.topNone)&(按位与):用来检查是否包含某个值
12345678910//OCif ((self.lineType & CellExLineTypeTopNone) == CellExLineTypeTopNone) {NSLog(@"包含CellExLineTypeTopNone");}//Swiftvar lineType: CellExLineType = [.topNone, .bottomNone]if lineType.contains(.bottomNone) {print("包含bottomNone")}^(异或):用来置反某个值(如果包含则剔除,如果不包含则添加)
123456789101112//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
的情况。
在系统中的许多Enum也是这么使用的,如UIViewAutoresizing、UIViewAnimationOptions等。
为什么要在枚举中使用位操作符?
- 在许多古老的微处理器上,位运算比加减运算略快,通常位运算比乘除法运算要快很多。在现代架构中,情况并非如此:位运算的运算速度通常与加法运算相同(仍然快于乘法运算)
- 可以给一个属性同时设置多个值
总结
~
(按位取反):对目标数字按位取反;在枚举中用于剔除某个值|
(按位或):对两个目标数字同位置上数字进行或运算;在枚举中用于添加某个值&
(按位与):对两个目标数字同位置上数字进行与运算;在枚举中用于判断是否包含某个值
^
(按位异或):对两个目标数字同位置上数字进行异或运算;在枚举中置反某个值>>
(右移):对目标数字按位右移x位<<
(左移):对目标数字按位左移x位