2023-11-09|閱讀時間 ‧ 約 2 分鐘

[swift]移位運算(Shift Operator)

以底下兩數字為例:

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


分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.