Generic Operator

>>=(_:_:)

Stores the result of shifting a value’s binary representation the specified number of digits to the right in the left-hand-side variable.

Declaration

static func >>= <Other>(lhs: inout Int, rhs: Other) where Other : BinaryInteger

Parameters

lhs

The value to shift.

rhs

The number of bits to shift lhs to the right.

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 left shift using abs(rhs).

  • Using a value for rhs that is greater than or equal to the bit width of lhs is an overshift. An overshift results in -1 for a negative value of lhs or 0 for a nonnegative value.

  • Using any other value for rhs performs a right shift on lhs by that amount.

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 right by two bits.

var x: UInt8 = 30                 // 0b00011110
x >>= 2
// x == 7                         // 0b00000111

If you use 11 as rhs, x is overshifted such that all of its bits are set to zero.

var y: UInt8 = 30                 // 0b00011110
y >>= 11
// y == 0                         // 0b00000000

Using a negative value as rhs is the same as performing a left shift using abs(rhs).

var a: UInt8 = 30                 // 0b00011110
a >>= -3
// a == 240                       // 0b11110000

var b: UInt8 = 30                 // 0b00011110
b <<= 3
// b == 240                       // 0b11110000

Right shift operations on negative values “fill in” the high bits with ones instead of zeros.

var q: Int8 = -30                 // 0b11100010
q >>= 2
// q == -8                        // 0b11111000

var r: Int8 = -30                 // 0b11100010
r >>= 11
// r == -1                        // 0b11111111

See Also

Bit Shift

static func << <Other>(Int, Other) -> Int

Returns the result of shifting a value’s binary representation the specified number of digits to the left.

static func << <RHS>(Int, RHS) -> Int

Returns the result of shifting a value’s binary representation the specified number of digits to the left.

static func <<= <Other>(inout Int, Other)

Stores the result of shifting a value’s binary representation the specified number of digits to the left in the left-hand-side variable.

static func >> <Other>(Int, Other) -> Int

Returns the result of shifting a value’s binary representation the specified number of digits to the right.

static func >> <RHS>(Int, RHS) -> Int

Returns the result of shifting a value’s binary representation the specified number of digits to the right.

static func &<< (Int, Int) -> Int

Returns the result of shifting a value’s binary representation the specified number of digits to the left, masking the shift amount to the type’s bit width.

static func &<< (Int, Int) -> Int

Returns the result of shifting a value’s binary representation the specified number of digits to the left, masking the shift amount to the type’s bit width.

static func &<< <Other>(Int, Other) -> Int

Returns the result of shifting a value’s binary representation the specified number of digits to the left, masking the shift amount to the type’s bit width.

static func &<<= (inout Int, Int)

Returns the result of shifting a value’s binary representation the specified number of digits to the left, masking the shift amount to the type’s bit width, and stores the result in the left-hand-side variable.

static func &<<= <Other>(inout Int, Other)

Returns the result of shifting a value’s binary representation the specified number of digits to the left, masking the shift amount to the type’s bit width, and stores the result in the left-hand-side variable.

static func &>> (Int, Int) -> Int

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.

static func &>> (Int, Int) -> Int

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.

static func &>> <Other>(Int, Other) -> Int

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.

static func &>>= (inout Int, Int)

Calculates 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, and stores the result in the left-hand-side variable.

static func &>>= <Other>(inout Int, Other)

Calculates 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, and stores the result in the left-hand-side variable.