欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎樣通過Objective-C的枚舉學習iOS中位操作.md-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)怎樣通過Objective-C的枚舉學習iOS中位操作.md的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

為南潯等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及南潯網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計制作、做網(wǎng)站、南潯網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

位操作

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

在此只討論iOS中的所有位操作的運算符,別的語言的相同含義的操作符號可能不同

位運算符(以下操作符皆同Objective-C)

位運算符一種包含下面幾種:

~(取反,一元操作符):它會對目標數(shù)字的二進制每位進行取反

let initialBits: UInt8 = 0b00001111let invertedBits = ~initialBits // equals 11110000

|(按位或):它會對兩個目標數(shù)字的相同位置數(shù)字進行或運算,規(guī)則: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)

&(按位與):它會對兩個目標數(shù)字的相同位置數(shù)字進行與運算,規(guī)則: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)

^(異或):它會對兩個目標數(shù)字的相同位置數(shù)字進行異或運算,如果不同則該位為1,否則該位為0。規(guī)則:如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)

移位

>>(右移):它會對目標數(shù)字按位右移x位

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

<<(左移):它會對目標數(shù)字按位左移x位(右邊補0)

let targetNum = 5 // 101print(targetNum << 2) //print 20//targetNum: 101//左移2位//result:  10100 (十進制 20)

枚舉中的位操作

通過上文我們了解了位操作的具體計算方式,接下來看一下在枚舉中的具體應(yīng)用。

枚舉中的應(yīng)用

定義枚舉

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) {}

在系統(tǒng)中的許多Enum也是這么使用的,如UIViewAutoresizing、UIViewAnimationOptions等。

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

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

感謝各位的閱讀!關(guān)于“怎樣通過Objective-C的枚舉學習iOS中位操作.md”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

新聞標題:怎樣通過Objective-C的枚舉學習iOS中位操作.md-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://chinadenli.net/article26/cojocg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT響應(yīng)式網(wǎng)站網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計移動網(wǎng)站建設(shè)做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站