struct UInt8
An 8bit unsigned integer value type.
Inheritance  FixedWidthInteger, UnsignedInteger, Codable, CustomReflectable, Hashable, SIMDScalar 

Associated Types 
public typealias IntegerLiteralType = UInt8
public typealias Magnitude = UInt8
public typealias SIMDMaskScalar = Int8

Nested Types  UInt8.Words, UInt8.SIMD2Storage, UInt8.SIMD4Storage, UInt8.SIMD8Storage, UInt8.SIMD16Storage, UInt8.SIMD32Storage, UInt8.SIMD64Storage 
Initializers
Creates an integer from the given floatingpoint value, rounding toward zero.
Any fractional part of the value passed as source
is removed, rounding
the value toward zero.
let x = Int(21.5)
// x == 21
let y = Int(21.5)
// y == 21
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 floatingpoint 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 floatingpoint value, rounding toward zero.
Any fractional part of the value passed as source
is removed, rounding
the value toward zero.
let x = Int(21.5)
// x == 21
let y = Int(21.5)
// y == 21
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 floatingpoint 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 floatingpoint value, rounding toward zero.
Any fractional part of the value passed as source
is removed, rounding
the value toward zero.
let x = Int(21.5)
// x == 21
let y = Int(21.5)
// y == 21
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 floatingpoint 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 floatingpoint value, rounding toward
zero. Any fractional part of the value passed as source
is removed.
let x = Int(21.5)
// x == 21
let y = Int(21.5)
// y == 21
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 floatingpoint 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
Construct with value v.value
.
Precondition:
v.value
can be represented as ASCII (0..<128).
Declaration
@inlinable public init(ascii v: Unicode.Scalar)
Creates an integer from its bigendian representation, changing the byte order if necessary.
 Parameter value: A value to use as the bigendian 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: Int8)
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
.
let x = Int8(clamping: 500)
// x == 127
// x == Int8.max
let y = UInt(clamping: 500)
// y == 0
 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 littleendian representation, changing the byte order if necessary.
 Parameter value: A value to use as the littleendian 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 signextending 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
leastsignificant bits of source
. For example, when converting a
16bit value to an 8bit type, only the lower 8 bits of source
are
used.
let p: Int16 = 500
// 'p' has a binary representation of 11111110_00001100
let q = Int8(truncatingIfNeeded: p)
// q == 12
// 'q' has a binary representation of 00001100
When the bit width of T
is less than this type's bit width, the result
is signextended 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 (09
).
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
:
Int(" 100") // Includes whitespace
Int("2150") // Invalid format
Int("ff6600") // Characters out of bounds
Int("10000000000000000000000000") // Out of range
 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 (09
) or letters
(az
or AZ
). Parsing of the string is case insensitive.
let x = Int("123")
// x == 123
let y = Int("123", radix: 8)
// y == 83
let y = Int("+123", radix: 8)
// y == +83
let z = Int("07b", radix: 16)
// z == 123
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
:
Int(" 100") // Includes whitespace
Int("2150") // Invalid format
Int("ff6600") // Characters out of bounds
Int("zzzzzzzzzzzzz", radix: 36) // Out of range
Declaration
@inlinable public init?<S>(_ text: S, radix: Int = 10) where S: StringProtocol
Creates an integer from the given floatingpoint 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:
let x = Int(exactly: 21.0)
// x == Optional(21)
let y = Int(exactly: 21.5)
// y == nil
 Parameter source: A floatingpoint value to convert to an integer.
Declaration
public init?(exactly source: Float)
Creates an integer from the given floatingpoint 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:
let x = Int(exactly: 21.0)
// x == Optional(21)
let y = Int(exactly: 21.5)
// y == nil
 Parameter source: A floatingpoint value to convert to an integer.
Declaration
public init?(exactly source: Double)
Creates an integer from the given floatingpoint 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:
let x = Int(exactly: 21.0)
// x == Optional(21)
let y = Int(exactly: 21.5)
// y == nil
 Parameter source: A floatingpoint value to convert to an integer.
Declaration
public init?(exactly source: Float80)
Creates an integer from the given floatingpoint 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:
let x = Int(exactly: 21.0)
// x == Optional(21)
let y = Int(exactly: 21.5)
// y == nil
 Parameter source: A floatingpoint value to convert to an integer.
Declaration
@inlinable public init?<T>(exactly source: T) where T: BinaryFloatingPoint
Instance Variables
The bigendian 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 bigendian platform, for any
integer x
, x == x.bigEndian
.
Declaration
var bigEndian: Self
The number of bits in the binary representation of this value.
Declaration
var bitWidth: Int
A representation of this integer with the byte order swapped.
Declaration
var byteSwapped: UInt8
A mirror that reflects the UInt8
instance.
Declaration
var customMirror: Mirror
A custom playground Quick Look for the UInt8
instance.
Declaration
var customPlaygroundQuickLook: _PlaygroundQuickLook
The number of leading zeros in this value's binary representation.
For example, in an integer type with a bitWidth
value of 8,
the number 31 has three leading zeros.
let x: Int8 = 0b0001_1111
// x == 31
// x.leadingZeroBitCount == 3
Declaration
var leadingZeroBitCount: Int
The littleendian 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 littleendian platform, for any
integer x
, x == x.littleEndian
.
Declaration
var littleEndian: Self
The magnitude of this value.
Every unsigned integer is its own magnitude, so for any value x
,
x == x.magnitude
.
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: Self
The number of bits equal to 1 in this value's binary representation.
For example, in a fixedwidth integer type with a bitWidth
value of 8,
the number 31 has five bits equal to 1.
let x: Int8 = 0b0001_1111
// x == 31
// x.nonzeroBitCount == 5
Declaration
var nonzeroBitCount: Int
The number of scalars, or elements, in the vector.
Declaration
var scalarCount: Int
The number of scalars, or elements, in the vector.
Declaration
var scalarCount: Int
The number of scalars, or elements, in the vector.
Declaration
var scalarCount: Int
The number of scalars, or elements, in the vector.
Declaration
var scalarCount: Int
The number of scalars, or elements, in the vector.
Declaration
var scalarCount: Int
The number of scalars, or elements, in the vector.
Declaration
var scalarCount: Int
The number of trailing zeros in this value's binary representation.
For example, the number 8 has three trailing zeros.
let x = Int8(bitPattern: 0b1111_1000)
// x == 8
// x.trailingZeroBitCount == 3
Declaration
var trailingZeroBitCount: Int
A collection containing the words of this value's binary representation, in order from the least significant to most significant.
Declaration
var words: UInt8.Words
Subscripts
Accesses the element at the specified index.
 Parameter index: The index of the element to access.
index
must be in the range0..<scalarCount
.
Declaration
public subscript(index: Int) > UInt8
Accesses the element at the specified index.
 Parameter index: The index of the element to access.
index
must be in the range0..<scalarCount
.
Declaration
public subscript(index: Int) > UInt8
Accesses the element at the specified index.
 Parameter index: The index of the element to access.
index
must be in the range0..<scalarCount
.
Declaration
public subscript(index: Int) > UInt8
Accesses the element at the specified index.
 Parameter index: The index of the element to access.
index
must be in the range0..<scalarCount
.
Declaration
public subscript(index: Int) > UInt8
Accesses the element at the specified index.
 Parameter index: The index of the element to access.
index
must be in the range0..<scalarCount
.
Declaration
public subscript(index: Int) > UInt8
Accesses the element at the specified index.
 Parameter index: The index of the element to access.
index
must be in the range0..<scalarCount
.
Declaration
public subscript(index: Int) > UInt8
Instance Methods
Returns the sum of this value and the given value, along with a Boolean value indicating whether overflow occurred in the operation.
 Parameter rhs: The value to add to this value.
Declaration
public func addingReportingOverflow(_ other: UInt8) > (partialValue: UInt8, overflow: Bool)
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
public func dividedReportingOverflow(by other: UInt8) > (partialValue: UInt8, overflow: Bool)
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
doublewidth integer. The
high
component of the value carries the sign, if the type is signed.
Declaration
@inlinable public func dividingFullWidth(_ dividend: (high: UInt8, low: UInt8.Magnitude)) > (quotient: UInt8, remainder: UInt8)
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:
let x: UInt8 = 100
let y: UInt8 = 20
let result = x.multipliedFullWidth(by: y)
// result.high == 0b00000111
// result.low == 0b11010000
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 doublewidth integer; that
is, using result.high
as the high byte and result.low
as the low byte
of a UInt16
instance.
let z = UInt16(result.high) << 8  UInt16(result.low)
// z == 2000
 Parameter other: The value to multiply this value by.
Declaration
@inlinable public func multipliedFullWidth(by other: UInt8) > (high: UInt8, low: UInt8.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:
let x: Int8 = 48
let y: Int8 = 40
let result = x.multipliedFullWidth(by: y)
// result.high == 8
// result.low == 128
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 doublewidth integer; that
is, using result.high
as the high byte and result.low
as the low byte
of an Int16
instance.
let z = Int16(result.high) << 8  Int16(result.low)
// z == 1920
 Parameter other: The value to multiply this value by.
Declaration
public func multipliedFullWidth(by other: Self) > (high: Self, low: Self.Magnitude)
Returns the product of this value and the given value, along with a Boolean value indicating whether overflow occurred in the operation.
 Parameter rhs: The value to multiply by this value.
Declaration
public func multipliedReportingOverflow(by other: UInt8) > (partialValue: UInt8, overflow: Bool)
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
public func remainderReportingOverflow(dividingBy other: UInt8) > (partialValue: UInt8, overflow: Bool)
Returns 1
if this value is negative and 1
if it's positive;
otherwise, 0
.
Declaration
@inlinable public func signum() > UInt8
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
public func subtractingReportingOverflow(_ other: UInt8) > (partialValue: UInt8, overflow: Bool)
Type Variables
The number of bits used for the underlying binary representation of values of this type.
The bit width of an UInt8
instance is 8.
Declaration
var bitWidth: Int
A Boolean value indicating whether this type is a signed integer type.
This property is always false
for unsigned 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
public static func %(lhs: UInt8, rhs: UInt8) > UInt8
Divides the first value by the second and stores the remainder in the lefthandside 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
public static func %=(lhs: inout UInt8, rhs: UInt8)
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:
let x: UInt8 = 5 // 0b00000101
let y: UInt8 = 14 // 0b00001110
let z = x & y // 0b00000100
// z == 4
Declaration
public static func &(lhs: UInt8, rhs: UInt8) > UInt8
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.
let x: Int8 = 10 &* 5
// x == 50
let y: Int8 = 10 &* 50
// y == 12 (after overflow)
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 lefthandside 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.
var x: Int8 = 10
x &*= 5
// x == 50
var y: Int8 = 10
y &*= 50
// y == 12 (after overflow)
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.
let x: Int8 = 10 &+ 21
// x == 31
let y: Int8 = 100 &+ 121
// y == 35 (after overflow)
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 lefthandside 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.
var x: Int8 = 10
x &+= 21
// x == 31
var y: Int8 = 100
y &+= 121
// y == 35 (after overflow)
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.
let x: UInt8 = 21 & 10
// x == 11
let y: UInt8 = 10 & 21
// y == 245 (after overflow)
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 lefthandside 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.
var x: Int8 = 21
x &= 10
// x == 11
var y: UInt8 = 10
y &= 21
// y == 245 (after overflow)
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 8bit,
unsigned integer type. If you use 2
as the righthandside 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: UInt8, rhs: UInt8) > UInt8
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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 lefthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 &<<=(lhs: inout UInt8, rhs: UInt8)
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 lefthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 lefthandside 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:
var x: UInt8 = 5 // 0b00000101
let y: UInt8 = 14 // 0b00001110
x &= y // 0b00000100
Declaration
public static func &=(lhs: inout UInt8, rhs: UInt8)
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 8bit,
unsigned integer type. If you use 2
as the righthandside 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: UInt8, rhs: UInt8) > UInt8
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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 lefthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 &>>=(lhs: inout UInt8, rhs: UInt8)
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 lefthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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.
let x: Int8 = 21
let y: Int = 1000000
Int(x) * y // 21000000
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
public static func *(lhs: UInt8, rhs: UInt8) > UInt8
Multiplies two values and stores the result in the lefthandside 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
public static func *=(lhs: inout UInt8, rhs: UInt8)
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.
let x: Int8 = 21
let y: Int = 1000000
Int(x) + y // 1000021
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
public static func +(lhs: UInt8, rhs: UInt8) > UInt8
Adds two values and stores the result in the lefthandside 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
public static func +=(lhs: inout UInt8, rhs: UInt8)
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.
let x: UInt8 = 21
let y: UInt = 1000000
y  UInt(x) // 999979
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
public static func (lhs: UInt8, rhs: UInt8) > UInt8
Subtracts the second value from the first and stores the difference in the lefthandside 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
public static func =(lhs: inout UInt8, rhs: UInt8)
Returns the quotient of dividing the first value by the second.
For integer types, any remainder of the division is discarded.
let x = 21 / 5
// x == 4
Declaration
public static func /(lhs: UInt8, rhs: UInt8) > UInt8
Divides the first value by the second and stores the quotient in the lefthandside variable.
For integer types, any remainder of the division is discarded.
var x = 21
x /= 5
// x == 4
Declaration
public static func /=(lhs: inout UInt8, rhs: UInt8)
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
public static func <(lhs: UInt8, rhs: UInt8) > Bool
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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 lefthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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)
.
var a: UInt8 = 30 // 0b00011110
a <<= 3
// a == 3 // 0b00000011
var b: UInt8 = 30 // 0b00011110
b >>= 3
// b == 3 // 0b00000011
Declaration
public static func <<=<Other>(lhs: inout Self, rhs: Other) where Other: BinaryInteger
Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.
Declaration
public static func <=(lhs: UInt8, rhs: UInt8) > Bool
Returns a Boolean value indicating whether two values are equal.
Equality is the inverse of inequality. For any values a
and b
,
a == b
implies that a != b
is false
.
Declaration
public static func ==(lhs: UInt8, rhs: UInt8) > Bool
Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.
Declaration
public static func >(lhs: UInt8, rhs: UInt8) > Bool
Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.
Declaration
public static func >=(lhs: UInt8, rhs: UInt8) > Bool
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 8bit,
unsigned integer type. If you use 2
as the righthandside 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 lefthandside 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 8bit,
unsigned integer type. If you use 2
as the righthandside 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
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:
let x: UInt8 = 5 // 0b00000101
let y: UInt8 = 14 // 0b00001110
let z = x ^ y // 0b00001011
// z == 11
Declaration
public static func ^(lhs: UInt8, rhs: UInt8) > UInt8
Stores the result of performing a bitwise XOR operation on the two given values in the lefthandside 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:
var x: UInt8 = 5 // 0b00000101
let y: UInt8 = 14 // 0b00001110
x ^= y // 0b00001011
Declaration
public static func ^=(lhs: inout UInt8, rhs: UInt8)
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 {
print(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 {
print(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 {
print(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 {
print(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:
let x: UInt8 = 5 // 0b00000101
let y: UInt8 = 14 // 0b00001110
let z = x  y // 0b00001111
// z == 15
Declaration
public static func (lhs: UInt8, rhs: UInt8) > UInt8
Stores the result of performing a bitwise OR operation on the two given values in the lefthandside 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:
var x: UInt8 = 5 // 0b00000101
let y: UInt8 = 14 // 0b00001110
x = y // 0b00001111
Declaration
public static func =(lhs: inout UInt8, rhs: UInt8)
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()