Operator (Default implementation)swift 6.0.1Swift
<<(_:_:)
Returns the result of shifting a value’s binary representation the specified number of digits to the left.
static func << <Other>(lhs: Self, rhs: Other) -> Self where Other : BinaryInteger
Parameters
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 usingabs(rhs)
.Using a value for
rhs
that is greater than or equal to the bit width oflhs
is an overshift, resulting in zero.Using any other value for
rhs
performs a left shift onlhs
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 left by two bits.
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.
let z = x << 11
// z == 0 // 0b00000000
Using a negative value as rhs
is the same as performing a right shift with abs(rhs)
.
let a = x << -3
// a == 3 // 0b00000011
let b = x >> 3
// b == 3 // 0b00000011
Implements
static func << <RHS>(lhs: Self, rhs: RHS
) -> Self Returns the result of shifting a value’s binary representation the specified number of digits to the left.
Other members in extension
Type members
init?(String
) Creates a new integer value from the given string.
init<T>(T
) Creates an integer from the given floating-point value, rounding toward zero. Any fractional part of the value passed as
source
is removed.init?<S>(S, radix: Int
) Creates a new integer value from the given string and radix.
init(bigEndian: Self
) Creates an integer from its big-endian representation, changing the byte order if necessary.
init<Other>(clamping: Other
) Creates a new instance with the representable value that’s closest to the given integer.
init?<T>(exactly: T
) Creates an integer from the given floating-point value, if it can be represented exactly.
init(littleEndian: Self
) Creates an integer from its little-endian representation, changing the byte order if necessary.
init<T>(truncatingIfNeeded: T
) Creates a new instance from the bit pattern of the given instance by truncating or sign-extending if needed to fit this type.
static func random(in: ClosedRange<Self>
) -> Self Returns a random value within the specified range.
static func random(in: Range<Self>
) -> Self Returns a random value within the specified range.
static func random<T>(in: ClosedRange<Self>, using: inout T
) -> Self Returns a random value within the specified range, using the given generator as a source for randomness.
static func random<T>(in: Range<Self>, using: inout T
) -> Self Returns a random value within the specified range, using the given generator as a source for randomness.
static func &* (lhs: Self, rhs: Self
) -> Self static func &*= (lhs: inout Self, rhs: Self
) Multiplies two values and stores the result in the left-hand-side variable, wrapping any overflow.
static func &+ (lhs: Self, rhs: Self
) -> Self Returns the sum of the two given values, wrapping the result in case of any overflow.
static func &+= (lhs: inout Self, rhs: Self
) Adds two values and stores the result in the left-hand-side variable, wrapping any overflow.
static func &- (lhs: Self, rhs: Self
) -> Self Returns the difference of the two given values, wrapping the result in case of any overflow.
static func &-= (lhs: inout Self, rhs: Self
) Subtracts the second value from the first and stores the difference in the left-hand-side variable, wrapping any overflow.
static func &<< <Other>(lhs: Self, rhs: Other
) -> Self 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 &<< (lhs: Self, rhs: Self
) -> Self 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>(lhs: inout Self, rhs: 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 &>> <Other>(lhs: Self, rhs: Other
) -> Self 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 &>> (lhs: Self, rhs: Self
) -> Self 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>(lhs: inout Self, rhs: 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.
static func <<= <Other>(lhs: inout Self, rhs: Other
) static func >> <Other>(lhs: Self, rhs: Other
) -> Self Returns the result of shifting a value’s binary representation the specified number of digits to the right.
static func >>= <Other>(lhs: inout Self, rhs: Other
) static func ~ (x: Self
) -> Self Returns the inverse of the bits set in the argument.
Instance members
var bigEndian: Self
The big-endian representation of this integer.
var bitWidth: Int
The number of bits in the binary representation of this value.
var littleEndian: Self
The little-endian representation of this integer.
func multipliedFullWidth(by: Self
) -> (high: Self, low: Self.Magnitude)