如何通過Objective-C的枚舉學(xué)習(xí)iOS中位操作.md詳解
開篇
今天在修改項(xiàng)目的時(shí)候,看見enum中出現(xiàn)了<<操作符(位操作),之前對(duì)這個(gè)一直都不了解。這次趁著項(xiàng)目比較清閑,抽出時(shí)間來全面了解一下位操作。
位操作
位操作是對(duì)二進(jìn)制數(shù)逐位進(jìn)行運(yùn)算或移位。它共包含兩種操作:位運(yùn)算和移位。下面就詳細(xì)的了解一下這兩種操作。
在此只討論iOS中的所有位操作的運(yùn)算符,別的語言的相同含義的操作符號(hào)可能不同
位運(yùn)算符(以下操作符皆同Objective-C)
位運(yùn)算符一種包含下面幾種:
~(取反,一元操作符):它會(huì)對(duì)目標(biāo)數(shù)字的二進(jìn)制每位進(jìn)行取反
let initialBits: UInt8 = 0b00001111 let invertedBits = ~initialBits // equals 11110000
|(按位或):它會(huì)對(duì)兩個(gè)目標(biāo)數(shù)字的相同位置數(shù)字進(jìn)行或運(yùn)算,規(guī)則: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 (十進(jìn)制 7)
&(按位與):它會(huì)對(duì)兩個(gè)目標(biāo)數(shù)字的相同位置數(shù)字進(jìn)行與運(yùn)算,規(guī)則: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 (十進(jìn)制 4)
^(異或):它會(huì)對(duì)兩個(gè)目標(biāo)數(shù)字的相同位置數(shù)字進(jìn)行異或運(yùn)算,如果不同則該位為1,否則該位為0。規(guī)則:如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 (十進(jìn)制 3)
移位
>>(右移):它會(huì)對(duì)目標(biāo)數(shù)字按位右移x位
let targetNum = 5 // 101 print(targetNum >> 2) //print 1 //targetNum: 101 //右移2位 //result: 1 (十進(jìn)制 1)
<<(左移):它會(huì)對(duì)目標(biāo)數(shù)字按位左移x位(右邊補(bǔ)0)
let targetNum = 5 // 101 print(targetNum << 2) //print 20 //targetNum: 101 //左移2位 //result: 10100 (十進(jìn)制 20)
枚舉中的位操作
通過上文我們了解了位操作的具體計(jì)算方式,接下來看一下在枚舉中的具體應(yīng)用。
枚舉中的應(yīng)用
定義枚舉
OC
typedef NS_OPTIONS(NSInteger, CellExLineType) {
CellExLineTypeTopLong = 0,
CellExLineTypeTopNone = 1 << 0, //十進(jìn)制 1
CellExLineTypeBottomLong = 1 << 1, //十進(jìn)制 2
CellExLineTypeBottomNone = 1 << 2, //十進(jìn)制 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)
}
位操作在枚舉中的作用
~(取反):用來剔除某個(gè)值
//OC self.lineType = CellExLineTypeTopNone; self.lineType |= CellExLineTypeBottomNone; self.lineType = self.lineType & ~CellExLineTypeTopNone; //self.lineTye 只包含CellExLineTypeBottomNone //Swift var lineType: CellExLineType = [.topNone, .bottomNone] lineType.remove(.topNone)
|(按位或):用來添加某個(gè)值
//OC self.lineType = CellExLineTypeTopNone; //self.lineType 包含CellExLineTypeTopNone self.lineType = self.lineType | CellExLineTypeBottomNone; //self.lineType 包含CellExLineTypeTopNone和CellExLineTypeBottomNone //Swift var lineType: CellExLineType = [.bottomNone] lineType.insert(.topNone)
&(按位與):用來檢查是否包含某個(gè)值
//OC
if ((self.lineType & CellExLineTypeTopNone) == CellExLineTypeTopNone) {
NSLog(@"包含CellExLineTypeTopNone");
}
//Swift
var lineType: CellExLineType = [.topNone, .bottomNone]
if lineType.contains(.bottomNone) {
print("包含bottomNone")
}
^(異或):用來置反某個(gè)值(如果包含則剔除,如果不包含則添加)
//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)
}
在枚舉中使用位操作我們可以方便的給一個(gè)屬性值賦值多個(gè)值,比如下面的代碼給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) {
}
在系統(tǒng)中的許多Enum也是這么使用的,如UIViewAutoresizing、UIViewAnimationOptions等。
為什么要在枚舉中使用位操作符?
- 在許多古老的微處理器上,位運(yùn)算比加減運(yùn)算略快,通常位運(yùn)算比乘除法運(yùn)算要快很多。在現(xiàn)代架構(gòu)中,情況并非如此:位運(yùn)算的運(yùn)算速度通常與加法運(yùn)算相同(仍然快于乘法運(yùn)算)
- 可以給一個(gè)屬性同時(shí)設(shè)置多個(gè)值
總結(jié)
- ~(按位取反):對(duì)目標(biāo)數(shù)字按位取反;在枚舉中用于剔除某個(gè)值
- |(按位或):對(duì)兩個(gè)目標(biāo)數(shù)字同位置上數(shù)字進(jìn)行或運(yùn)算;在枚舉中用于添加某個(gè)值
- &(按位與):對(duì)兩個(gè)目標(biāo)數(shù)字同位置上數(shù)字進(jìn)行與運(yùn)算;在枚舉中用于判斷是否包含某個(gè)值
- ^(按位異或):對(duì)兩個(gè)目標(biāo)數(shù)字同位置上數(shù)字進(jìn)行異或運(yùn)算;在枚舉中置反某個(gè)值
- >>(右移):對(duì)目標(biāo)數(shù)字按位右移x位
- <<(左移):對(duì)目標(biāo)數(shù)字按位左移x位
參考
- Bitwise Operators and Bit Masks
- SO
- How to create NS_OPTIONS-style bitmask enumerations in Swift?
- 位操作
- Advanced Operators
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS開發(fā)之topLayoutGuide和bottomLayoutGuide的使用小技巧分享
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之topLayoutGuide和bottomLayoutGuide使用的一些小技巧,需要的朋友可以參考下2017-11-11
iOS中定位(location manager )出現(xiàn)log日志的解決辦法
這篇文章主要給大家介紹了關(guān)于iOS中定位(location manager )出現(xiàn)log日志的解決辦法,文中通過示例代碼將解決的辦法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
iOS大文件的分片上傳和斷點(diǎn)上傳的實(shí)現(xiàn)代碼
這篇文章主要介紹了iOS大文件的分片上傳和斷點(diǎn)上傳的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-12-12
ios8 UITableView設(shè)置 setSeparatorInset UIEdgeInsetsZero不起作用的解決
這篇文章主要介紹了ios8 UITableView設(shè)置 setSeparatorInset UIEdgeInsetsZero不起作用的解決辦法(去掉15px空白間距)的相關(guān)資料,需要的朋友可以參考下2016-02-02
iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06
解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問題
這篇文章主要介紹了解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問題,需要的朋友可以參考下2016-12-12
IOS collectionViewCell防止復(fù)用的兩種方法
這篇文章主要介紹了IOS collectionViewCell防止復(fù)用的兩種方法的相關(guān)資料,需要的朋友可以參考下2016-11-11

