Generic Operator

<<(_:_:)

Returns the result of shifting a value’s binary representation the specified number of digits to the left. ある値のバイナリ表現を指定された桁数だけ左にシフトする結果を返します。

Declaration 宣言

static func << <Other>(lhs: UInt8, rhs: Other) -> UInt8 where Other : BinaryInteger

Parameters パラメータ

lhs

The value to shift. シフトする値。

rhs

The number of bits to shift lhs to the left. このビット数をlhsから左へとシフトします。

Discussion 解説

The << operator performs a smart shift, which defines a result for a shift of any value. <<演算子は、ある賢いシフトを実行します、それは何らかの値のシフトに対する結果を定義します。

  • Using a negative value for rhs performs a right shift using abs(rhs). rhsに負の値を使うことは、abs(rhs)を使って右シフトを実行します。

  • Using a value for rhs that is greater than or equal to the bit width of lhs is an overshift, resulting in zero. rhslhsのビット幅より大きいか等しい値を使うことは、オーバーシフト、ゼロという結果になります。

  • Using any other value for rhs performs a left shift on lhs by that amount. rhsに何か他の値を使うことは、左シフトをlhs上でその量だけ行います。

The following example defines x as an instance of UInt8, an 8-bit, unsigned integer type. If you use 2 as the right-hand-side value in an operation on x, the value is shifted left by two bits. 以下の例は、xUInt8のインスタンス、ある8ビットの、符号なし整数型として定義します。あなたが2x上での演算の右手側の値として使うならば、値は2ビットだけ左にシフトされます。


let x: UInt8 = 30                 // 0b00011110
let y = x << 2
// y == 120                       // 0b01111000

If you use 11 as rhs, x is overshifted such that all of its bits are set to zero. あなたが11rhsとして使うならば、xは、それのビットのすべてがゼロに設定されるような、オーバーシフトをされます。


let z = x << 11
// z == 0                         // 0b00000000

Using a negative value as rhs is the same as performing a right shift with abs(rhs). 負の値をrhsとして使うことは、abs(rhs)を使って右シフトを実行するのと同じです。


let a = x << -3
// a == 3                         // 0b00000011
let b = x >> 3
// b == 3                         // 0b00000011