操作符
<<=(_:
<<=(_:_:)
將值的二進製表示左移指定位數的結果存儲在左側變量中。
聲明
static func <<= <Other>(lhs: inout Self, rhs: Other) where Other : BinaryInteger
參數
lhs要轉移的值。
rhs將
lhs向左移動的位數。
詳述
<< 運算符執行 smart shift ,它定義了任何值移位的結果。
-
使用
rhs的負值使用abs(rhs)執行右移。 -
使用大於或等於
lhs位寬的rhs的值是overshift,結果為零。 -
為
rhs使用任何其他值會在lhs上執行該數量的左移。
以下示例將 x 定義為 UInt8 的一個實例,這是一個 8 位無符號整數類型。如果在對 x 的操作中使用 2 作為右側值,則該值將左移兩位。
var x: UInt8 = 30 // 0b00011110
x <<= 2
// x == 120 // 0b01111000
如果將 11 用作 rhs ,則 x 會過度移位,使其所有位都設置為零。
var y: UInt8 = 30 // 0b00011110
y <<= 11
// y == 0 // 0b00000000
使用負值作為 rhs 與使用 abs(rhs) 執行右移相同。
var a: UInt8 = 30 // 0b00011110
a <<= -3
// a == 3 // 0b00000011
var b: UInt8 = 30 // 0b00011110
b >>= 3
// b == 3 // 0b00000011
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift UInt <<(_:_:)用法及代碼示例
- Swift UInt +(_:)用法及代碼示例
- Swift UInt &<<(_:_:)用法及代碼示例
- Swift UInt leadingZeroBitCount用法及代碼示例
- Swift UInt init(_:radix:)用法及代碼示例
- Swift UInt quotientAndRemainder(dividingBy:)用法及代碼示例
- Swift UInt &(_:_:)用法及代碼示例
- Swift UInt &*(_:_:)用法及代碼示例
- Swift UInt init(truncatingIfNeeded:)用法及代碼示例
- Swift UInt *=(_:_:)用法及代碼示例
- Swift UInt %(_:_:)用法及代碼示例
- Swift UInt &-(_:_:)用法及代碼示例
- Swift UInt +=(_:_:)用法及代碼示例
- Swift UInt ==(_:_:)用法及代碼示例
- Swift UInt |=(_:_:)用法及代碼示例
- Swift UInt >>=(_:_:)用法及代碼示例
- Swift UInt multipliedFullWidth(by:)用法及代碼示例
- Swift UInt !=(_:_:)用法及代碼示例
- Swift UInt +(_:_:)用法及代碼示例
- Swift UInt nonzeroBitCount用法及代碼示例
- Swift UInt ..<(_:)用法及代碼示例
- Swift UInt ...(_:_:)用法及代碼示例
- Swift UInt init(exactly:)用法及代碼示例
- Swift UInt &+=(_:_:)用法及代碼示例
- Swift UInt ^(_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 UInt <<=(_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
