UnsignedInteger

protocol UnsignedInteger

An integer type that can represent only nonnegative values.

Inheritance BinaryInteger
Conforming Types UInt, UInt16, UInt32, UInt64, UInt8

Default Implementations

func !=(lhs: Self, rhs: Other) -> Bool

Returns a Boolean value indicating whether the two given values are not equal.

You can check the inequality of instances of any BinaryInteger types using the not-equal-to operator (!=). For example, you can test whether the first UInt8 value in a string's UTF-8 encoding is not equal to the first UInt32 value in its Unicode scalar view:

let gameName = "Red Light, Green Light"
if let firstUTF8 = gameName.utf8.first,
    let firstScalar = gameName.unicodeScalars.first?.value {
    print("First code values are different: \(firstUTF8 != firstScalar)")
}
// Prints "First code values are different: false"

Declaration

public static func !=<Other>(lhs: Self, rhs: Other) -> Bool where Other: BinaryInteger
func !=(lhs: Self, rhs: Self) -> Bool

Declaration

public static func !=(lhs: Self, rhs: Self) -> Bool
func &(lhs: Self, rhs: Self) -> Self

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: Self, rhs: Self) -> Self
func <(lhs: Self, rhs: Other) -> Bool

Returns a Boolean value indicating whether the value of the first argument is less than that of the second argument.

You can compare instances of any BinaryInteger types using the less-than operator (<), even if the two instances are of different types.

Declaration

public static func <<Other>(lhs: Self, rhs: Other) -> Bool where Other: BinaryInteger
func <<(lhs: Self, rhs: RHS) -> Self

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 <<<RHS>(lhs: Self, rhs: RHS) -> Self where RHS: BinaryInteger
func <=(lhs: Self, rhs: Other) -> Bool

Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.

You can compare instances of any BinaryInteger types using the less-than-or-equal-to operator (<=), even if the two instances are of different types.

Declaration

public static func <=<Other>(lhs: Self, rhs: Other) -> Bool where Other: BinaryInteger
func <=(lhs: Self, rhs: Self) -> Bool

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: Self, rhs: Self) -> Bool
func ==(lhs: Self, rhs: Other) -> Bool

Returns a Boolean value indicating whether the two given values are equal.

You can check the equality of instances of any BinaryInteger types using the equal-to operator (==). For example, you can test whether the first UInt8 value in a string's UTF-8 encoding is equal to the first UInt32 value in its Unicode scalar view:

let gameName = "Red Light, Green Light"
if let firstUTF8 = gameName.utf8.first,
    let firstScalar = gameName.unicodeScalars.first?.value {
    print("First code values are equal: \(firstUTF8 == firstScalar)")
}
// Prints "First code values are equal: true"

Declaration

public static func ==<Other>(lhs: Self, rhs: Other) -> Bool where Other: BinaryInteger
func >(lhs: Self, rhs: Other) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.

You can compare instances of any BinaryInteger types using the greater-than operator (>), even if the two instances are of different types.

Declaration

public static func ><Other>(lhs: Self, rhs: Other) -> Bool where Other: BinaryInteger
func >(lhs: Self, rhs: Self) -> 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: Self, rhs: Self) -> Bool
func >=(lhs: Self, rhs: Other) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.

You can compare instances of any BinaryInteger types using the greater-than-or-equal-to operator (>=), even if the two instances are of different types.

Declaration

public static func >=<Other>(lhs: Self, rhs: Other) -> Bool where Other: BinaryInteger
func >=(lhs: Self, rhs: Self) -> 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: Self, rhs: Self) -> Bool
func >>(lhs: Self, rhs: RHS) -> Self

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 >><RHS>(lhs: Self, rhs: RHS) -> Self where RHS: BinaryInteger
func ^(lhs: Self, rhs: Self) -> Self

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: Self, rhs: Self) -> Self
func advanced(by n: Int) -> Self

Returns a value that is offset the specified distance from this value.

Use the advanced(by:) method in generic code to offset a value by a specified distance. If you're working directly with numeric values, use the addition operator (+) instead of this method.

For a value x, a distance n, and a value y = x.advanced(by: n), x.distance(to: y) == n.

  • Parameter n: The distance to advance this value.

Declaration

@inlinable public func advanced(by n: Int) -> Self
var description

A textual representation of this value.

Declaration

var description: String
func distance(to other: Self) -> Int

Returns the distance from this value to the given value, expressed as a stride.

For two values x and y, and a distance n = x.distance(to: y), x.advanced(by: n) == y.

  • Parameter other: The value to calculate the distance to.

Declaration

@inlinable public func distance(to other: Self) -> Int
init init()

Creates a new value equal to zero.

Declaration

public init()
func isMultiple(of other: Self) -> Bool

Returns true if this value is a multiple of the given value, and false otherwise.

For two integers a and b, a is a multiple of b if there exists a third integer q such that a = q*b. For example, 6 is a multiple of 3 because 6 = 2*3. Zero is a multiple of everything because 0 = 0*x for any integer x.

Two edge cases are worth particular attention:

  • Parameter other: The value to test.

Declaration

@inlinable public func isMultiple(of other: Self) -> Bool
func quotientAndRemainder(dividingBy rhs: Self) -> (quotient: Self, remainder: Self)

Returns the quotient and remainder of this value divided by the given value.

Use this method to calculate the quotient and remainder of a division at the same time.

let x = 1_000_000
let (q, r) = x.quotientAndRemainder(dividingBy: 933)
// q == 1071
// r == 757
  • Parameter rhs: The value to divide this value by.

Declaration

@inlinable public func quotientAndRemainder(dividingBy rhs: Self) -> (quotient: Self, remainder: Self)
func signum() -> Self

Returns -1 if this value is negative and 1 if it's positive; otherwise, 0.

Declaration

@inlinable public func signum() -> Self
func |(lhs: Self, rhs: Self) -> Self

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: Self, rhs: Self) -> Self