温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎样通过Objective-C的枚举学习iOS中位操作.md

发布时间:2021-09-26 16:31:30 来源:亿速云 阅读:133 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关怎样通过Objective-C的枚举学习iOS中位操作.md的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

位操作

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

在此只讨论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等。

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

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

感谢各位的阅读!关于“怎样通过Objective-C的枚举学习iOS中位操作.md”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI