Operator 演算子

&>>(_:_:)

Returns the result of shifting a value’s binary representation the specified number of digits to the right, masking the shift amount to the type’s bit width. ある値のバイナリ表現を指定された桁数だけ右にシフトする結果を返します、シフト量をその型のもつビット幅にマスクしています。

Declaration 宣言

static func &>> (lhs: UInt8, rhs: UInt8) -> UInt8

Parameters パラメータ

lhs

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

rhs

The number of bits to shift lhs to the right. If rhs is outside the range 0..<lhs.bitWidth, it is masked to produce a value within that range. このビット数をlhsから右へとシフトします。rhsが範囲0..<lhs.bitWidthの外側ならば、それはマスクされてその範囲内の値を生み出します。

Discussion 解説

Use the masking right shift operator (&>>) when you need to perform a shift and are sure that the shift amount is in the range 0..<lhs.bitWidth. Before shifting, the masking right shift operator masks the shift to this range. The shift is performed using this masked value. マスク右シフト演算子(&>>)を、あなたがあるシフトを実行する、そしてシフト量が範囲0..<lhs.bitWidthの中であるのを確実にする必要がある場合に使ってください。シフトする前に、マスク右シフト演算子は、シフトをマスクしてこの範囲にします。シフトは、このマスクされた値を使って実行されます。

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 shift amount requires no masking. 以下の例は、xUInt8のインスタンス、ある8ビットの、符号なし整数型として定義します。あなたが2を右手側の値としてxに関する演算において使うならば、シフト量はマスクを必要としません。


let x: UInt8 = 30                 // 0b00011110
let y = x &>> 2
// y == 7                         // 0b00000111

However, if you use 8 as the shift amount, the method first masks the shift amount to zero, and then performs the shift, resulting in no change to the original value. しかしながら、あなたが8をシフト量として使うならば、メソッドは最初にそのシフト量をマスクしてゼロにして、それからシフトを実行して、元の値に対して変更しないという結果になります。


let z = x &>> 8
// z == 30                        // 0b00011110

If the bit width of the shifted integer type is a power of two, masking is performed using a bitmask; otherwise, masking is performed using a modulo operation. シフトされる整数型のビット幅が2の冪ならば、マスクはビットマスクを使って実行されます;そうでなければ、マスクはモジュロ演算を使って実行されます。

Relationships 関係

From Protocol 由来プロトコル