[swift]位元運算(Bitwise operator)

小黑
發佈於APP
2023/11/09閱讀時間約 1 分鐘

以底下兩數字為例:

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


and運算

let andResult = bin1 & bin2
print(String(andResult,radix: 2))//00001000


or運算

let orResult = bin1 | bin2
print(String(orResult,radix: 2))//11111111


xor運算

let xorResult = bin1 ^ bin2        
print(String(xorResult,radix: 2))//11110111


not運算

let notBin1Result = ~bin1
print(String(notBin1Result,radix: 2))//00000000

let notBin2Result = ~bin2
print(String(notBin2Result,radix: 2))//11110111


nor運算

let norResult = ~(bin1 | bin2)
print(String(norResult,radix: 2))//00000000



7會員
78內容數
嗨,我是一名程式設計師,會在這分享開發與學習紀錄。
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容