当前位置: 移动技术网 > IT编程>移动开发>IOS > 如何通过Objective-C的枚举学习iOS中位操作.md详解

如何通过Objective-C的枚举学习iOS中位操作.md详解

2019年07月23日  | 移动技术网IT编程  | 我要评论

陌小彩,一战到底 题库,姚明简历

开篇

今天在修改项目的时候,看见enum中出现了<<操作符(位操作),之前对这个一直都不了解。这次趁着项目比较清闲,抽出时间来全面了解一下位操作。

位操作

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

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

位运算符(以下操作符皆同objective-c)

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

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

let initialbits: uint8 = 0b00001111
let invertedbits = ~initialbits // equals 11110000

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

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

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

let targetnum = 5 // 101
let targetnum2 = 6 // 110
print(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 // 101
let targetnum2 = 6 // 110
print(targetnum ^ targetnum2) //print 3
//targetnum: 101
//targetnum2: 110
//result:  011 (十进制 3)

移位

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

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

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

let targetnum = 5 // 101
print(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)
}

位操作在枚举中的作用

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

//oc
self.linetype = cellexlinetypetopnone;
self.linetype |= cellexlinetypebottomnone;
self.linetype = self.linetype & ~cellexlinetypetopnone; //self.linetye 只包含cellexlinetypebottomnone
//swift
var linetype: cellexlinetype = [.topnone, .bottomnone]
linetype.remove(.topnone)

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

//oc
self.linetype = cellexlinetypetopnone; //self.linetype 包含cellexlinetypetopnone
self.linetype = self.linetype | cellexlinetypebottomnone; //self.linetype 包含cellexlinetypetopnone和cellexlinetypebottomnone
//swift
var linetype: cellexlinetype = [.bottomnone]
linetype.insert(.topnone)

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

//oc
if ((self.linetype & cellexlinetypetopnone) == cellexlinetypetopnone) {
 nslog(@"包含cellexlinetypetopnone");
}
//swift
var linetype: cellexlinetype = [.topnone, .bottomnone]
if linetype.contains(.bottomnone) {
 print("包含bottomnone")
}

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

//oc
self.linetype = cellexlinetypetopnone | cellexlinetypebottomnone; //self.linetype 包含cellexlinetypetopnone和cellexlinetypebottomnone
self.linetype = self.linetype ^ cellexlinetypetopnone; //self.linetye 只包含cellexlinetypebottomnone
self.linetype = self.linetype ^ cellexlinetypetopnone; //self.linetype 包含cellexlinetypetopnone和cellexlinetypebottomnone
//swift
var 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");
 }
}
//swift
var linetype: cellexlinetype = [.topnone, .bottomnone]
if linetype.contains(.topnone) {
 linetype.remove(.topnone)
}
if linetype.contains(.bottomnone) {
 
}

在系统中的许多enum也是这么使用的,如uiviewautoresizing、uiviewanimationoptions等。

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

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

总结

  • ~(按位取反):对目标数字按位取反;在枚举中用于剔除某个值
  • |(按位或):对两个目标数字同位置上数字进行或运算;在枚举中用于添加某个值
    • &(按位与):对两个目标数字同位置上数字进行与运算;在枚举中用于判断是否包含某个值
  • ^(按位异或):对两个目标数字同位置上数字进行异或运算;在枚举中置反某个值
  • >>(右移):对目标数字按位右移x位
  • <<(左移):对目标数字按位左移x位

参考

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网