struct
Int32
A 32-bit signed integer value type.
Inheritance | FixedWidthInteger, SignedInteger, Codable, CustomReflectable, Hashable, SIMDScalar |
---|---|
Associated Types |
|
Nested Types | Int32.Words, Int32.SIMD2Storage, Int32.SIMD4Storage, Int32.SIMD8Storage, Int32.SIMD16Storage, Int32.SIMD32Storage, Int32.SIMD64Storage |
Initializers
Creates an integer from the given floating-point value, rounding toward zero.
Any fractional part of the value passed as source
is removed, rounding
the value toward zero.
If source
is outside the bounds of this type after rounding toward
zero, a runtime error may occur.
let
z
=
UInt
(-
21.5
)
// Error: ...the result would be less than UInt.min
- Parameter source: A floating-point value to convert to an integer.
source
must be representable in this type after rounding toward zero.
Declaration
public
init
(
_
source
:
Float
)
Creates an integer from the given floating-point value, rounding toward zero.
Any fractional part of the value passed as source
is removed, rounding
the value toward zero.
If source
is outside the bounds of this type after rounding toward
zero, a runtime error may occur.
let
z
=
UInt
(-
21.5
)
// Error: ...the result would be less than UInt.min
- Parameter source: A floating-point value to convert to an integer.
source
must be representable in this type after rounding toward zero.
Declaration
public
init
(
_
source
:
Double
)
Creates an integer from the given floating-point value, rounding toward zero.
Any fractional part of the value passed as source
is removed, rounding
the value toward zero.
If source
is outside the bounds of this type after rounding toward
zero, a runtime error may occur.
let
z
=
UInt
(-
21.5
)
// Error: ...the result would be less than UInt.min
- Parameter source: A floating-point value to convert to an integer.
source
must be representable in this type after rounding toward zero.
Declaration
public
init
(
_
source
:
Float80
)
Creates an integer from the given floating-point value, rounding toward
zero. Any fractional part of the value passed as source
is removed.
If source
is outside the bounds of this type after rounding toward
zero, a runtime error may occur.
let
z
=
UInt
(-
21.5
)
// Error: ...outside the representable range
- Parameter source: A floating-point value to convert to an integer.
source
must be representable in this type after rounding toward zero.
Declaration
@
inlinable
public
init
<
T
>
(
_
source
:
T
)
where
T
:
BinaryFloatingPoint
Creates an integer from its big-endian representation, changing the byte order if necessary.
- Parameter value: A value to use as the big-endian representation of the new integer.
Declaration
@
inlinable
public
init
(
bigEndian
value
:
Self
)
Creates a new instance with the same memory representation as the given value.
This initializer does not perform any range or overflow checking. The
resulting instance may not have the same numeric value as
bitPattern
---it is only guaranteed to use the same pattern of bits in
its binary representation.
- Parameter x: A value to use as the source of the new instance's binary representation.
Declaration
public
init
(
bitPattern
x
:
UInt32
)
Creates a new instance with the representable value that's closest to the given integer.
If the value passed as source
is greater than the maximum representable
value in this type, the result is the type's max
value. If source
is
less than the smallest representable value in this type, the result is
the type's min
value.
In this example, x
is initialized as an Int8
instance by clamping
500
to the range -128...127
, and y
is initialized as a UInt
instance by clamping -500
to the range 0...UInt.max
.
- Parameter source: An integer to convert to this type.
Declaration
@
inlinable
public
init
<
Other
>
(
clamping
source
:
Other
)
where
Other
:
BinaryInteger
Creates a new instance by decoding from the given decoder.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
- Parameter decoder: The decoder to read data from.
Declaration
public
init
(
from
decoder
:
Decoder
)
throws
Creates an integer from its little-endian representation, changing the byte order if necessary.
- Parameter value: A value to use as the little-endian representation of the new integer.
Declaration
@
inlinable
public
init
(
littleEndian
value
:
Self
)
Creates a new instance from the bit pattern of the given instance by truncating or sign-extending if needed to fit this type.
When the bit width of T
(the type of source
) is equal to or greater
than this type's bit width, the result is the truncated
least-significant bits of source
. For example, when converting a
16-bit value to an 8-bit type, only the lower 8 bits of source
are
used.
When the bit width of T
is less than this type's bit width, the result
is sign-extended to fill the remaining bits. That is, if source
is
negative, the result is padded with ones; otherwise, the result is
padded with zeros.
let
u
:
Int8
=
21
// 'u' has a binary representation of 00010101
let
v
=
Int16
(
truncatingIfNeeded
:
u
)
// v == 21
// 'v' has a binary representation of 00000000_00010101
let
w
:
Int8
= -
21
// 'w' has a binary representation of 11101011
let
x
=
Int16
(
truncatingIfNeeded
:
w
)
// x == -21
// 'x' has a binary representation of 11111111_11101011
let
y
=
UInt16
(
truncatingIfNeeded
:
w
)
// y == 65515
// 'y' has a binary representation of 11111111_11101011
- Parameter source: An integer to convert to this type.
Declaration
@
inlinable
public
init
<
T
>
(
truncatingIfNeeded
source
:
T
)
where
T
:
BinaryInteger
Creates a new integer value from the given string.
The string passed as description
may begin with a plus or minus sign
character (+
or -
), followed by one or more numeric digits (0-9
).
let
x
=
Int
(
"123"
)
// x == 123
If description
is in an invalid format, or if the value it denotes in
base 10 is not representable, the result is nil
. For example, the
following conversions result in nil
:
- Parameter description: The ASCII representation of a number.
Declaration
@
inlinable
public
init
?(
_
description
:
String
)
Creates a new integer value from the given string and radix.
The string passed as text
may begin with a plus or minus sign character
(+
or -
), followed by one or more numeric digits (0-9
) or letters
(a-z
or A-Z
). Parsing of the string is case insensitive.
If text
is in an invalid format or contains characters that are out of
bounds for the given radix
, or if the value it denotes in the given
radix
is not representable, the result is nil
. For example, the
following conversions result in nil
:
Declaration
@
inlinable
public
init
?
<
S
>
(
_
text
:
S
,
radix
:
Int
=
10
)
where
S
:
StringProtocol
Creates an integer from the given floating-point value, if it can be represented exactly.
If the value passed as source
is not representable exactly, the result
is nil
. In the following example, the constant x
is successfully
created from a value of 21.0
, while the attempt to initialize the
constant y
from 21.5
fails:
- Parameter source: A floating-point value to convert to an integer.
Declaration
public
init
?(
exactly
source
:
Float
)
Creates an integer from the given floating-point value, if it can be represented exactly.
If the value passed as source
is not representable exactly, the result
is nil
. In the following example, the constant x
is successfully
created from a value of 21.0
, while the attempt to initialize the
constant y
from 21.5
fails:
- Parameter source: A floating-point value to convert to an integer.
Declaration
public
init
?(
exactly
source
:
Double
)
Creates an integer from the given floating-point value, if it can be represented exactly.
If the value passed as source
is not representable exactly, the result
is nil
. In the following example, the constant x
is successfully
created from a value of 21.0
, while the attempt to initialize the
constant y
from 21.5
fails:
- Parameter source: A floating-point value to convert to an integer.
Declaration
public
init
?(
exactly
source
:
Float80
)
Creates an integer from the given floating-point value, if it can be represented exactly.
If the value passed as source
is not representable exactly, the result
is nil
. In the following example, the constant x
is successfully
created from a value of 21.0
, while the attempt to initialize the
constant y
from 21.5
fails:
- Parameter source: A floating-point value to convert to an integer.
Declaration
@
inlinable
public
init
?
<
T
>
(
exactly
source
:
T
)
where
T
:
BinaryFloatingPoint
Instance Variables
The big-endian representation of this integer.
If necessary, the byte order of this value is reversed from the typical
byte order of this integer type. On a big-endian platform, for any
integer x
, x == x.bigEndian
.
Declaration
var
bigEndian
:
Self
A custom playground Quick Look for the Int32
instance.
Declaration
var
customPlaygroundQuickLook
:
_PlaygroundQuickLook
The little-endian representation of this integer.
If necessary, the byte order of this value is reversed from the typical
byte order of this integer type. On a little-endian platform, for any
integer x
, x == x.littleEndian
.
Declaration
var
littleEndian
:
Self
The magnitude of this value.
For any numeric value x
, x.magnitude
is the absolute value of x
.
You can use the magnitude
property in operations that are simpler to
implement in terms of unsigned values, such as printing the value of an
integer, which is just printing a '-' character in front of an absolute
value.
let
x
= -
200
// x.magnitude == 200
The global abs(_:)
function provides more familiar syntax when you need
to find an absolute value. In addition, because abs(_:)
always returns
a value of the same type, even in a generic context, using the function
instead of the magnitude
property is encouraged.
Declaration
var
magnitude
:
UInt32
A collection containing the words of this value's binary representation, in order from the least significant to most significant.
Negative values are returned in two's complement representation, regardless of the type's underlying implementation.
Declaration
var
words
:
Int32.Words
Subscripts
Instance Methods
Returns the quotient obtained by dividing this value by the given value, along with a Boolean value indicating whether overflow occurred in the operation.
Dividing by zero is not an error when using this method. For a value x
,
the result of x.dividedReportingOverflow(by: 0)
is (x, true)
.
- Parameter rhs: The value to divide this value by.
Declaration
Returns a tuple containing the quotient and remainder of dividing the given value by this value.
The resulting quotient must be representable within the bounds of the
type. If the quotient of dividing dividend
by this value is too large
to represent in the type, a runtime error may occur.
- Parameter dividend: A tuple containing the high and low parts of a
double-width integer. The
high
component of the value carries the sign, if the type is signed.
Declaration
@
inlinable
public
func
dividingFullWidth
(
_
dividend
: (
high
:
Int32
,
low
:
Int32.Magnitude
)) -
>
(
quotient
:
Int32
,
remainder
:
Int32
)
Encodes this value into the given encoder.
This function throws an error if any values are invalid for the given encoder's format.
- Parameter encoder: The encoder to write data to.
Declaration
public
func
encode
(
to
encoder
:
Encoder
)
throws
Hashes the essential components of this value by feeding them into the given hasher.
- Parameter hasher: The hasher to use when combining the components of this instance.
Declaration
@
inlinable
public
func
hash
(
into
hasher
:
inout
Hasher
)
Returns a tuple containing the high and low parts of the result of multiplying this value by the given value.
Use this method to calculate the full result of a product that would
otherwise overflow. Unlike traditional truncating multiplication, the
multipliedFullWidth(by:)
method returns a tuple
containing both the high
and low
parts of the product of this value and
other
. The following example uses this method to multiply two UInt8
values that normally overflow when multiplied:
The product of x
and y
is 2000, which is too large to represent in a
UInt8
instance. The high
and low
properties of the result
value
represent 2000 when concatenated to form a double-width integer; that
is, using result.high
as the high byte and result.low
as the low byte
of a UInt16
instance.
- Parameter other: The value to multiply this value by.
Declaration
@
inlinable
public
func
multipliedFullWidth
(
by
other
:
Int32
) -
>
(
high
:
Int32
,
low
:
Int32.Magnitude
)
Returns a tuple containing the high and low parts of the result of multiplying this value by the given value.
Use this method to calculate the full result of a product that would
otherwise overflow. Unlike traditional truncating multiplication, the
multipliedFullWidth(by:)
method returns a tuple containing both the
high
and low
parts of the product of this value and other
. The
following example uses this method to multiply two Int8
values that
normally overflow when multiplied:
The product of x
and y
is -1920
, which is too large to represent in
an Int8
instance. The high
and low
compnents of the result
value
represent -1920
when concatenated to form a double-width integer; that
is, using result.high
as the high byte and result.low
as the low byte
of an Int16
instance.
- Parameter other: The value to multiply this value by.
Declaration
public
func
multipliedFullWidth
(
by
other
:
Self
) -
>
(
high
:
Self
,
low
:
Self
.
Magnitude
)
Returns the remainder after dividing this value by the given value, along with a Boolean value indicating whether overflow occurred during division.
Dividing by zero is not an error when using this method. For a value x
,
the result of x.remainderReportingOverflow(dividingBy: 0)
is
(x, true)
.
- Parameter rhs: The value to divide this value by.
Declaration
Returns -1
if this value is negative and 1
if it's positive;
otherwise, 0
.
Declaration
@
inlinable
public
func
signum
() -
>
Int32
Returns the difference obtained by subtracting the given value from this value, along with a Boolean value indicating whether overflow occurred in the operation.
- Parameter rhs: The value to subtract from this value.
Declaration
Type Variables
The number of bits used for the underlying binary representation of values of this type.
The bit width of a Int32
instance is 32.
Declaration
var
bitWidth
:
Int
A Boolean value indicating whether this type is a signed integer type.
This property is always true
for signed integer types.
Declaration
var
isSigned
:
Bool
Type Methods
Returns the remainder of dividing the first value by the second.
The result of the remainder operator (%
) has the same sign as lhs
and
has a magnitude less than rhs.magnitude
.
let
x
=
22
%
5
// x == 2
let
y
=
22
% -
5
// y == 2
let
z
= -
22
% -
5
// z == -2
For any two integers a
and b
, their quotient q
, and their remainder
r
, a == b * q + r
.
Declaration
Divides the first value by the second and stores the remainder in the left-hand-side variable.
The result has the same sign as lhs
and has a magnitude less than
rhs.magnitude
.
var
x
=
22
x
%=
5
// x == 2
var
y
=
22
y
%= -
5
// y == 2
var
z
= -
22
z
%= -
5
// z == -2
Declaration
Returns the result of performing a bitwise AND operation on the two given values.
A bitwise AND operation results in a value that has each bit set to 1
where both of its arguments have that bit set to 1
. For example:
Declaration
Returns the product of the two given values, wrapping the result in case of any overflow.
The overflow multiplication operator (&*
) discards any bits that
overflow the fixed width of the integer type. In the following example,
the product of 10
and 50
is greater than the maximum representable
Int8
value, so the result is the partial value after discarding the
overflowing bits.
For more about arithmetic with overflow operators, see Overflow Operators in The Swift Programming Language.
Declaration
public
static
func
&
*(
lhs
:
Self
,
rhs
:
Self
) -
>
Self
Multiplies two values and stores the result in the left-hand-side variable, wrapping any overflow.
The masking multiplication assignment operator (&*=
) silently wraps
any overflow that occurs during the operation. In the following example,
the product of 10
and 50
is greater than the maximum representable
Int8
value, so the result is the partial value after discarding the
overflowing bits.
For more about arithmetic with overflow operators, see Overflow Operators in The Swift Programming Language.
Declaration
public
static
func
&
*=(
lhs
:
inout
Self
,
rhs
:
Self
)
Returns the sum of the two given values, wrapping the result in case of any overflow.
The overflow addition operator (&+
) discards any bits that overflow the
fixed width of the integer type. In the following example, the sum of
100
and 121
is greater than the maximum representable Int8
value,
so the result is the partial value after discarding the overflowing
bits.
For more about arithmetic with overflow operators, see Overflow Operators in The Swift Programming Language.
Declaration
public
static
func
&
+(
lhs
:
Self
,
rhs
:
Self
) -
>
Self
Adds two values and stores the result in the left-hand-side variable, wrapping any overflow.
The masking addition assignment operator (&+=
) silently wraps any
overflow that occurs during the operation. In the following example, the
sum of 100
and 121
is greater than the maximum representable Int8
value, so the result is the partial value after discarding the
overflowing bits.
For more about arithmetic with overflow operators, see Overflow Operators in The Swift Programming Language.
Declaration
public
static
func
&
+=(
lhs
:
inout
Self
,
rhs
:
Self
)
Returns the difference of the two given values, wrapping the result in case of any overflow.
The overflow subtraction operator (&-
) discards any bits that overflow
the fixed width of the integer type. In the following example, the
difference of 10
and 21
is less than zero, the minimum representable
UInt
value, so the result is the partial value after discarding the
overflowing bits.
For more about arithmetic with overflow operators, see Overflow Operators in The Swift Programming Language.
Declaration
public
static
func
&
-(
lhs
:
Self
,
rhs
:
Self
) -
>
Self
Subtracts the second value from the first and stores the difference in the left-hand-side variable, wrapping any overflow.
The masking subtraction assignment operator (&-=
) silently wraps any
overflow that occurs during the operation. In the following example, the
difference of 10
and 21
is less than zero, the minimum representable
UInt
value, so the result is the result is the partial value after
discarding the overflowing bits.
For more about arithmetic with overflow operators, see Overflow Operators in The Swift Programming Language.
Declaration
public
static
func
&
-=(
lhs
:
inout
Self
,
rhs
:
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.
Use the masking left 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 left shift operator
masks the shift to this range. The shift is performed using this masked
value.
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.
let
x
:
UInt8
=
30
// 0b00011110
let
y
=
x
&
<
<
2
// y == 120 // 0b01111000
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.
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.
Declaration
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.
Use the masking left 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 left shift operator
masks the shift to this range. The shift is performed using this masked
value.
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.
let
x
:
UInt8
=
30
// 0b00011110
let
y
=
x
&
<
<
2
// y == 120 // 0b01111000
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.
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.
Declaration
public
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.
Use the masking left 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 left shift operator
masks the shift to this range. The shift is performed using this masked
value.
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.
let
x
:
UInt8
=
30
// 0b00011110
let
y
=
x
&
<
<
2
// y == 120 // 0b01111000
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.
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.
Declaration
public
static
func
&
<
<
<
Other
>
(
lhs
:
Self
,
rhs
:
Other
) -
>
Self
where
Other
:
BinaryInteger
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.
The &<<=
operator performs a masking shift, where the value used as
rhs
is masked to produce a value in the range 0..<lhs.bitWidth
. The
shift is performed using this masked value.
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.
var
x
:
UInt8
=
30
// 0b00011110
x
&
<
<
=
2
// x == 120 // 0b01111000
However, if you pass 19
as rhs
, the method first bitmasks rhs
to
3
, and then uses that masked value as the number of bits to shift lhs
.
var
y
:
UInt8
=
30
// 0b00011110
y
&
<
<
=
19
// y == 240 // 0b11110000
Declaration
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.
The &<<=
operator performs a masking shift, where the value used as
rhs
is masked to produce a value in the range 0..<lhs.bitWidth
. The
shift is performed using this masked value.
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.
var
x
:
UInt8
=
30
// 0b00011110
x
&
<
<
=
2
// x == 120 // 0b01111000
However, if you pass 19
as rhs
, the method first bitmasks rhs
to
3
, and then uses that masked value as the number of bits to shift lhs
.
var
y
:
UInt8
=
30
// 0b00011110
y
&
<
<
=
19
// y == 240 // 0b11110000
Declaration
public
static
func
&
<
<
=
<
Other
>
(
lhs
:
inout
Self
,
rhs
:
Other
)
where
Other
:
BinaryInteger
Stores the result of performing a bitwise AND operation on the two given values in the left-hand-side variable.
A bitwise AND operation results in a value that has each bit set to 1
where both of its arguments have that bit set to 1
. For example:
Declaration
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.
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.
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.
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.
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.
Declaration
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.
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.
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.
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.
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.
Declaration
public
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.
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.
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.
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.
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.
Declaration
public
static
func
&
>
>
<
Other
>
(
lhs
:
Self
,
rhs
:
Other
) -
>
Self
where
Other
:
BinaryInteger
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.
The &>>=
operator performs a masking shift, where the value passed as
rhs
is masked to produce a value in the range 0..<lhs.bitWidth
. The
shift is performed using this masked value.
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.
var
x
:
UInt8
=
30
// 0b00011110
x
&
>
>
=
2
// x == 7 // 0b00000111
However, if you use 19
as rhs
, the operation first bitmasks rhs
to
3
, and then uses that masked value as the number of bits to shift lhs
.
var
y
:
UInt8
=
30
// 0b00011110
y
&
>
>
=
19
// y == 3 // 0b00000011
Declaration
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.
The &>>=
operator performs a masking shift, where the value passed as
rhs
is masked to produce a value in the range 0..<lhs.bitWidth
. The
shift is performed using this masked value.
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.
var
x
:
UInt8
=
30
// 0b00011110
x
&
>
>
=
2
// x == 7 // 0b00000111
However, if you use 19
as rhs
, the operation first bitmasks rhs
to
3
, and then uses that masked value as the number of bits to shift lhs
.
var
y
:
UInt8
=
30
// 0b00011110
y
&
>
>
=
19
// y == 3 // 0b00000011
Declaration
public
static
func
&
>
>
=
<
Other
>
(
lhs
:
inout
Self
,
rhs
:
Other
)
where
Other
:
BinaryInteger
Multiplies two values and produces their product.
The multiplication operator (*
) calculates the product of its two
arguments. For example:
2
*
3
// 6
100
*
21
// 2100
-
10
*
15
// -150
3.5
*
2.25
// 7.875
You cannot use *
with arguments of different types. To multiply values
of different types, convert one of the values to the other value's type.
The product of the two arguments must be representable in the arguments'
type. In the following example, the result of 21 * 21
is greater than
the maximum representable Int8
value:
x
*
21
// Overflow error
Note: Overflow checking is not performed in
-Ounchecked
builds.
If you want to opt out of overflow checking and wrap the result in case
of any overflow, use the overflow multiplication operator (&*
).
x
&
*
21
// -115
Declaration
Multiplies two values and stores the result in the left-hand-side variable.
The product of the two arguments must be representable in the arguments'
type. In the following example, the result of 21 * 21
is greater than
the maximum representable Int8
value:
var
x
:
Int8
=
21
x
*
21
// Overflow error
Note: Overflow checking is not performed in
-Ounchecked
builds.
Declaration
Adds two values and produces their sum.
The addition operator (+
) calculates the sum of its two arguments. For
example:
1
+
2
// 3
-
10
+
15
// 5
-
15
+ -
5
// -20
21.5
+
3.25
// 24.75
You cannot use +
with arguments of different types. To add values of
different types, convert one of the values to the other value's type.
The sum of the two arguments must be representable in the arguments'
type. In the following example, the result of 21 + 120
is greater than
the maximum representable Int8
value:
x
+
120
// Overflow error
Note: Overflow checking is not performed in
-Ounchecked
builds.
If you want to opt out of overflow checking and wrap the result in case
of any overflow, use the overflow addition operator (&+
).
x
&
+
120
// -115
Declaration
Adds two values and stores the result in the left-hand-side variable.
The sum of the two arguments must be representable in the arguments'
type. In the following example, the result of 21 + 120
is greater than
the maximum representable Int8
value:
var
x
:
Int8
=
21
x
+=
120
// Overflow error
Note: Overflow checking is not performed in
-Ounchecked
builds.
Declaration
Subtracts one value from another and produces their difference.
The subtraction operator (-
) calculates the difference of its two
arguments. For example:
8
-
3
// 5
-
10
-
5
// -15
100
- -
5
// 105
10.5
-
100.0
// -89.5
You cannot use -
with arguments of different types. To subtract values
of different types, convert one of the values to the other value's type.
The difference of the two arguments must be representable in the
arguments' type. In the following example, the result of 21 - 50
is
less than zero, the minimum representable UInt8
value:
x
-
50
// Overflow error
Note: Overflow checking is not performed in
-Ounchecked
builds.
If you want to opt out of overflow checking and wrap the result in case
of any overflow, use the overflow subtraction operator (&-
).
x
&
-
50
// 227
Declaration
Subtracts the second value from the first and stores the difference in the left-hand-side variable.
The difference of the two arguments must be representable in the
arguments' type. In the following example, the result of 21 - 50
is
less than zero, the minimum representable UInt8
value:
var
x
:
UInt8
=
21
x
-
50
// Overflow error
Note: Overflow checking is not performed in
-Ounchecked
builds.
Declaration
Returns a Boolean value indicating whether the value of the first argument is less than that of the second argument.
This function is the only requirement of the Comparable
protocol. The
remainder of the relational operator functions are implemented by the
standard library for any type that conforms to Comparable
.
Declaration
Returns the result of shifting a value's binary representation the specified number of digits to the left.
The <<
operator performs a smart shift, which defines a result for a
shift of any value.
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
Declaration
public
static
func
<
<
<
Other
>
(
lhs
:
Self
,
rhs
:
Other
) -
>
Self
where
Other
:
BinaryInteger
Stores the result of shifting a value's binary representation the specified number of digits to the left in the left-hand-side variable.
The <<
operator performs a smart shift, which defines a result for a
shift of any value.
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.
var
x
:
UInt8
=
30
// 0b00011110
x
<
<
=
2
// x == 120 // 0b01111000
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 right shift
with abs(rhs)
.
Declaration
public
static
func
<
<
=
<
Other
>
(
lhs
:
inout
Self
,
rhs
:
Other
)
where
Other
:
BinaryInteger
Returns the result of shifting a value's binary representation the specified number of digits to the right.
The >>
operator performs a smart shift, which defines a result for a
shift of any value.
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.
let
x
:
UInt8
=
30
// 0b00011110
let
y
=
x
>
>
2
// y == 7 // 0b00000111
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 left shift
using abs(rhs)
.
let
a
=
x
>
>
-
3
// a == 240 // 0b11110000
let
b
=
x
<
<
3
// b == 240 // 0b11110000
Right shift operations on negative values "fill in" the high bits with ones instead of zeros.
let
q
:
Int8
= -
30
// 0b11100010
let
r
=
q
>
>
2
// r == -8 // 0b11111000
let
s
=
q
>
>
11
// s == -1 // 0b11111111
Declaration
public
static
func
>
>
<
Other
>
(
lhs
:
Self
,
rhs
:
Other
) -
>
Self
where
Other
:
BinaryInteger
Stores the result of shifting a value's binary representation the specified number of digits to the right in the left-hand-side variable.
The >>=
operator performs a smart shift, which defines a result for a
shift of any value.
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)
.
Right shift operations on negative values "fill in" the high bits with ones instead of zeros.
Declaration
public
static
func
>
>
=
<
Other
>
(
lhs
:
inout
Self
,
rhs
:
Other
)
where
Other
:
BinaryInteger
Returns the result of performing a bitwise XOR operation on the two given values.
A bitwise XOR operation, also known as an exclusive OR operation, results
in a value that has each bit set to 1
where one or the other but not
both of its arguments had that bit set to 1
. For example:
Declaration
Stores the result of performing a bitwise XOR operation on the two given values in the left-hand-side variable.
A bitwise XOR operation, also known as an exclusive OR operation, results
in a value that has each bit set to 1
where one or the other but not
both of its arguments had that bit set to 1
. For example:
Declaration
Returns a random value within the specified range.
Use this method to generate an integer within a specific range. This
example creates three new values in the range 1..<100
.
for
_
in
1
...
3
{
(
Int
.
random
(
in
:
1
..
<
100
))
}
// Prints "53"
// Prints "64"
// Prints "5"
This method is equivalent to calling the version that takes a generator, passing in the system's default random generator.
- Parameter range: The range in which to create a random value.
range
must not be empty.
Declaration
@
inlinable
public
static
func
random
(
in
range
:
Range
<
Self
>
) -
>
Self
Returns a random value within the specified range.
Use this method to generate an integer within a specific range. This
example creates three new values in the range 1...100
.
for
_
in
1
...
3
{
(
Int
.
random
(
in
:
1
...
100
))
}
// Prints "53"
// Prints "64"
// Prints "5"
This method is equivalent to calling random(in:using:)
, passing in the
system's default random generator.
- Parameter range: The range in which to create a random value.
Declaration
@
inlinable
public
static
func
random
(
in
range
:
ClosedRange
<
Self
>
) -
>
Self
Returns a random value within the specified range, using the given generator as a source for randomness.
Use this method to generate an integer within a specific range when you
are using a custom random number generator. This example creates three
new values in the range 1..<100
.
for
_
in
1
...
3
{
(
Int
.
random
(
in
:
1
..
<
100
,
using
:
&
myGenerator
))
}
// Prints "7"
// Prints "44"
// Prints "21"
Note: The algorithm used to create random values may change in a future version of Swift. If you're passing a generator that results in the same sequence of integer values each time you run your program, that sequence may change when your program is compiled using a different version of Swift.
Declaration
@
inlinable
public
static
func
random
<
T
>
(
in
range
:
Range
<
Self
>
,
using
generator
:
inout
T
) -
>
Self
where
T
:
RandomNumberGenerator
Returns a random value within the specified range, using the given generator as a source for randomness.
Use this method to generate an integer within a specific range when you
are using a custom random number generator. This example creates three
new values in the range 1...100
.
for
_
in
1
...
3
{
(
Int
.
random
(
in
:
1
...
100
,
using
:
&
myGenerator
))
}
// Prints "7"
// Prints "44"
// Prints "21"
Declaration
@
inlinable
public
static
func
random
<
T
>
(
in
range
:
ClosedRange
<
Self
>
,
using
generator
:
inout
T
) -
>
Self
where
T
:
RandomNumberGenerator
Returns the result of performing a bitwise OR operation on the two given values.
A bitwise OR operation results in a value that has each bit set to 1
where one or both of its arguments have that bit set to 1
. For
example:
Declaration
Stores the result of performing a bitwise OR operation on the two given values in the left-hand-side variable.
A bitwise OR operation results in a value that has each bit set to 1
where one or both of its arguments have that bit set to 1
. For
example:
Declaration
Returns the inverse of the bits set in the argument.
The bitwise NOT operator (~
) is a prefix operator that returns a value
in which all the bits of its argument are flipped: Bits that are 1
in
the argument are 0
in the result, and bits that are 0
in the argument
are 1
in the result. This is equivalent to the inverse of a set. For
example:
let
x
:
UInt8
=
5
// 0b00000101
let
notX
= ~
x
// 0b11111010
Performing a bitwise NOT operation on 0 returns a value with every bit
set to 1
.
let
allOnes
= ~
UInt8
.
min
// 0b11111111
Complexity: O(1).
Declaration
prefix
public
static
func
~(
x
:
Self
) -
>
Self
Creates a vector with zero in all lanes.
Declaration
public
init
()