[swift]移位運算(Shift Operator)

閱讀時間約 1 分鐘

以底下兩數字為例:

let bin1:UInt8 = 0b11111111 //255
let bin2:UInt8 = 0b00001000 //8


向右位移:

let shiftRightBin1Result = bin1>>1
print(String(shiftRightBin1Result,radix: 2))//01111111

let shiftRightBin2Result = bin2>>2
print(String(shiftRightBin2Result,radix: 2))//00000010


向左位移:

let shiftLeftBin1Result = bin1<<1
print(String(shiftLeftBin1Result,radix: 2))//11111110

let shiftLeftBin2Result = bin2<<2
print(String(shiftLeftBin2Result,radix: 2))//00100000


8會員
119Content count
嗨,我是一名程式設計師,會在這分享開發與學習紀錄。
留言0
查看全部
發表第一個留言支持創作者!
小黑與程式的邂逅 的其他內容
以底下兩數字為例: let bin1:UInt8 = 0b11111111 //255 let bin2:UInt8 = 0b00001000 //8 and運算 let andResult = bin1 & bin2 print(String(andResult,radix: 2))//
二進制表示方式 let bin1:UInt8 = 0b11111111 //255 let bin2:UInt8 = 0b00001000 //8 八進制表示方式 let otc:UInt8 = 0o377 //255 十六進制表示方式 let hex:UInt8 = 0xff /
十進位轉二進位(Decimal to Binary) let dec = -813 let bin = String(UInt16(bitPattern: Int16(dec)), radix: 2) print(bin) // "1111110011010011" 十進位轉八進位(Deci
十進位轉二進位(Decimal to Binary) let dec = 813 let bin = String(dec, radix: 2) print(bin) // "1100101101" 十進位轉八進位(Decimal to Octal) let dec = 813 let o
取得當前時間字串 let currentDate = NSDate() let dateFormatter = DateFormatter() dateFormatter.locale = Locale.init(identifier: "zh_Hant_TW") dateFormatter.ti
取得當前時間 let currentDate = NSDate() 設定時間格式 樣式一: let dateFormatter = DateFormatter() dateFormatter.locale = Locale.init(identifier: "zh_Hant_TW") d
以底下兩數字為例: let bin1:UInt8 = 0b11111111 //255 let bin2:UInt8 = 0b00001000 //8 and運算 let andResult = bin1 & bin2 print(String(andResult,radix: 2))//
二進制表示方式 let bin1:UInt8 = 0b11111111 //255 let bin2:UInt8 = 0b00001000 //8 八進制表示方式 let otc:UInt8 = 0o377 //255 十六進制表示方式 let hex:UInt8 = 0xff /
十進位轉二進位(Decimal to Binary) let dec = -813 let bin = String(UInt16(bitPattern: Int16(dec)), radix: 2) print(bin) // "1111110011010011" 十進位轉八進位(Deci
十進位轉二進位(Decimal to Binary) let dec = 813 let bin = String(dec, radix: 2) print(bin) // "1100101101" 十進位轉八進位(Decimal to Octal) let dec = 813 let o
取得當前時間字串 let currentDate = NSDate() let dateFormatter = DateFormatter() dateFormatter.locale = Locale.init(identifier: "zh_Hant_TW") dateFormatter.ti
取得當前時間 let currentDate = NSDate() 設定時間格式 樣式一: let dateFormatter = DateFormatter() dateFormatter.locale = Locale.init(identifier: "zh_Hant_TW") d
你可能也想看
Thumbnail
重點摘要: 1.9 月降息 2 碼、進一步暗示年內還有 50 bp 降息 2.SEP 上修失業率預期,但快速的降息速率將有助失業率觸頂 3.未來幾個月經濟數據將繼續轉弱,經濟復甦的時點或是 1Q25 季底附近
Thumbnail
近期的「貼文發佈流程 & 版型大更新」功能大家使用了嗎? 新版式整體視覺上「更加凸顯圖片」,為了搭配這次的更新,我們推出首次貼文策展 ❤️ 使用貼文功能並完成這次的指定任務,還有機會獲得富士即可拍,讓你的美好回憶都可以用即可拍珍藏!
Thumbnail
字數算法 = string.count? 在swift算一個string的字數時候,很直覺的會想到用.count來算 let s = "這是幾個字呢".count print(s.count) // 6 毫無疑問的安心信賴6個字 表情符號的場合 let emoji = "😂" print
Thumbnail
一樣先來看官方文件 A view controller that provides access to documents or destinations outside your app’s sandbox. 其實就是讓你去讀取檔案App的東西 有兩種模式,Don’t copy the do
swift讀書筆記 https://docs.swift.org/swift-book/documentation/the-swift-programming-language/deinitialization/  class instance deallocated前會call deinitia
Thumbnail
swift讀書筆記 Documentation Edit descriptiondocs.swift.org objective-c 的init 會return value,swift 不會。 所有的property都必須在Init()裡面設定初始值,或設定stored property,這種
Thumbnail
其實16也會ㄏㄏ 相關討論: https://developer.apple.com/forums/thread/703145 重現步驟: 在iOS15以上,使用UIActivityViewController執行下列步驟會crash 1. 開啟UIActivityViewControlle
Thumbnail
一般來說在swift把Date轉成String,會使用dateFormat轉換 使用HH:mm轉成24小時制需注意的地方
三元條件運算子(Ternary Conditional Operator)是一種簡潔的寫法,用於在滿足條件時返回一個值,否則返回另一個值。 基本語法 其中,condition是要測試的條件,如果為true,則返回valueIfTrue,否則返回valueIfFalse。
Thumbnail
俄羅斯被踢出 SWIFT 支付系統,媒體形容是美、歐政府對俄羅斯發動了金融核戰。到底什麼是 SWIFT 它是如何運作的,鬼宿來告訴大家媒體沒有說清楚的事。可以這麼說:如果沒有 SWIFT;美元連在境內流動都將是不可能的任務。
本文推薦免費的3個網站,分為初學者、已有學過程式語言者兩類,鑑於PTT和自身的學習經驗,只要持之以恆的學習,Coding一定會給予回報。
Thumbnail
重點摘要: 1.9 月降息 2 碼、進一步暗示年內還有 50 bp 降息 2.SEP 上修失業率預期,但快速的降息速率將有助失業率觸頂 3.未來幾個月經濟數據將繼續轉弱,經濟復甦的時點或是 1Q25 季底附近
Thumbnail
近期的「貼文發佈流程 & 版型大更新」功能大家使用了嗎? 新版式整體視覺上「更加凸顯圖片」,為了搭配這次的更新,我們推出首次貼文策展 ❤️ 使用貼文功能並完成這次的指定任務,還有機會獲得富士即可拍,讓你的美好回憶都可以用即可拍珍藏!
Thumbnail
字數算法 = string.count? 在swift算一個string的字數時候,很直覺的會想到用.count來算 let s = "這是幾個字呢".count print(s.count) // 6 毫無疑問的安心信賴6個字 表情符號的場合 let emoji = "😂" print
Thumbnail
一樣先來看官方文件 A view controller that provides access to documents or destinations outside your app’s sandbox. 其實就是讓你去讀取檔案App的東西 有兩種模式,Don’t copy the do
swift讀書筆記 https://docs.swift.org/swift-book/documentation/the-swift-programming-language/deinitialization/  class instance deallocated前會call deinitia
Thumbnail
swift讀書筆記 Documentation Edit descriptiondocs.swift.org objective-c 的init 會return value,swift 不會。 所有的property都必須在Init()裡面設定初始值,或設定stored property,這種
Thumbnail
其實16也會ㄏㄏ 相關討論: https://developer.apple.com/forums/thread/703145 重現步驟: 在iOS15以上,使用UIActivityViewController執行下列步驟會crash 1. 開啟UIActivityViewControlle
Thumbnail
一般來說在swift把Date轉成String,會使用dateFormat轉換 使用HH:mm轉成24小時制需注意的地方
三元條件運算子(Ternary Conditional Operator)是一種簡潔的寫法,用於在滿足條件時返回一個值,否則返回另一個值。 基本語法 其中,condition是要測試的條件,如果為true,則返回valueIfTrue,否則返回valueIfFalse。
Thumbnail
俄羅斯被踢出 SWIFT 支付系統,媒體形容是美、歐政府對俄羅斯發動了金融核戰。到底什麼是 SWIFT 它是如何運作的,鬼宿來告訴大家媒體沒有說清楚的事。可以這麼說:如果沒有 SWIFT;美元連在境內流動都將是不可能的任務。
本文推薦免費的3個網站,分為初學者、已有學過程式語言者兩類,鑑於PTT和自身的學習經驗,只要持之以恆的學習,Coding一定會給予回報。