国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

如何通過Objective-C的枚舉學習iOS中位操作.md詳解

2020-07-26 02:21:02
字體:
來源:轉載
供稿:網友

開篇

今天在修改項目的時候,看見enum中出現了<<操作符(位操作),之前對這個一直都不了解。這次趁著項目比較清閑,抽出時間來全面了解一下位操作。

位操作

位操作是對二進制數逐位進行運算或移位。它共包含兩種操作:位運算和移位。下面就詳細的了解一下這兩種操作。

在此只討論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等。

為什么要在枚舉中使用位操作符?

  • 在許多古老的微處理器上,位運算比加減運算略快,通常位運算比乘除法運算要快很多。在現代架構中,情況并非如此:位運算的運算速度通常與加法運算相同(仍然快于乘法運算)
  • 可以給一個屬性同時設置多個值

總結

  • ~(按位取反):對目標數字按位取反;在枚舉中用于剔除某個值
  • |(按位或):對兩個目標數字同位置上數字進行或運算;在枚舉中用于添加某個值
    • &(按位與):對兩個目標數字同位置上數字進行與運算;在枚舉中用于判斷是否包含某個值
  • ^(按位異或):對兩個目標數字同位置上數字進行異或運算;在枚舉中置反某個值
  • >>(右移):對目標數字按位右移x位
  • <<(左移):對目標數字按位左移x位

參考

  • Bitwise Operators and Bit Masks
  • SO
  • How to create NS_OPTIONS-style bitmask enumerations in Swift?
  • 位操作
  • Advanced Operators

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇庆市| 山西省| 甘孜县| 九寨沟县| 温州市| 电白县| 无为县| 嘉兴市| 汶川县| 上栗县| 荔波县| 丹棱县| 平顺县| 潜江市| 鲁甸县| 鄂托克前旗| 佛教| 曲沃县| 马关县| 托里县| 开鲁县| 白水县| 武山县| 会宁县| 偃师市| 景德镇市| 吴川市| 肥西县| 闽清县| 安达市| 大城县| 互助| 常山县| 明水县| 保山市| 额济纳旗| 辉县市| 鹰潭市| 筠连县| 辉县市| 三明市|