Structureattaswift.bigint 5.4.1BigInt
BigUInt
An arbitary precision unsigned integer type, also known as a “big integer”.
BigUInt.swift:16struct BigUInt
Operations on big integers never overflow, but they may take a long time to execute. The amount of memory (and address space) available is the only constraint to the magnitude of these numbers.
This particular big integer type uses base-2^64 digits to represent integers; you can think of it as a wrapper around Array<UInt64>
. (In fact, BigUInt
only uses an array if there are more than two digits.)
Citizens in BigInt
Conformances
protocol AdditiveArithmetic
A type with values that support addition and subtraction.
protocol BinaryInteger
An integer type with a binary representation.
protocol Comparable
A type that can be compared using the relational operators
<
,<=
,>=
, and>
.protocol CustomPlaygroundDisplayConvertible
A type that supplies a custom description for playground logging.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Decodable
A type that can decode itself from an external representation.
protocol Encodable
A type that can encode itself to an external representation.
protocol Equatable
A type that can be compared for value equality.
protocol ExpressibleByExtendedGraphemeClusterLiteral
A type that can be initialized with a string literal containing a single extended grapheme cluster.
protocol ExpressibleByIntegerLiteral
A type that can be initialized with an integer literal.
protocol ExpressibleByStringLiteral
A type that can be initialized with a string literal.
protocol ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single Unicode scalar value.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Numeric
A type with values that support multiplication.
protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
protocol Strideable<Stride>
A type representing continuous, one-dimensional values that can be offset and measured.
protocol UnsignedInteger
An integer type that can represent only nonnegative values.
Types
Typealiases
typealias Stride
A type that can represent the distance between two values ofa
BigUInt
.typealias Word
The type representing a digit in
BigUInt
’s underlying number system.
Type members
init(
) Initializes a new BigUInt with value 0.
init(Data
) Initializes an integer from the bits stored inside a piece of
Data
. The data is assumed to be in network (big-endian) byte order.init(UnsafeRawBufferPointer
) Initialize a BigInt from bytes accessed from an UnsafeRawBufferPointer
init<T>(T
) init<T>(T
) init?<S>(S, radix: Int
) Initialize a big integer from an ASCII representation in a given radix. Numerals above
9
are represented by letters from the English alphabet.init<T>(clamping: T
) init?<T>(exactly: T
) init?<T>(exactly: T
) init(extendedGraphemeClusterLiteral: String
) Initialize a new big integer from an extended grapheme cluster. The cluster must consist of a decimal digit.
init(from: Decoder
) throws init(integerLiteral: UInt64
) Initialize a new big integer from an integer literal.
init(stringLiteral: StringLiteralType
) Initialize a new big integer from a decimal number represented by a string literal of arbitrary length. The string must contain only decimal digits.
init<T>(truncatingIfNeeded: T
) init(unicodeScalarLiteral: UnicodeScalar
) Initialize a new big integer from a Unicode scalar. The scalar must represent a decimal digit.
init(words: [Word]
) Initializes a new BigUInt with the specified digits. The digits are ordered from least to most significant.
init<Words>(words: Words
) static let directMultiplicationLimit: Int
Multiplication switches to an asymptotically better recursive algorithm when arguments have more words than this limit.
static var isSigned: Bool
static func compare(BigUInt, BigUInt
) -> ComparisonResult Compare
a
tob
and return anNSComparisonResult
indicating their order.static func randomInteger(lessThan: BigUInt
) -> BigUInt Create a uniformly distributed random unsigned integer that’s less than the specified limit.
static func randomInteger<RNG>(lessThan: BigUInt, using: inout RNG
) -> BigUInt Create a uniformly distributed random unsigned integer that’s less than the specified limit.
static func randomInteger(withExactWidth: Int
) -> BigUInt Create a big unsigned integer consisting of
width-1
uniformly distributed random bits followed by a one bit.static func randomInteger<RNG>(withExactWidth: Int, using: inout RNG
) -> BigUInt Create a big unsigned integer consisting of
width-1
uniformly distributed random bits followed by a one bit.static func randomInteger(withMaximumWidth: Int
) -> BigUInt Create a big unsigned integer consisting of
width
uniformly distributed random bits.static func randomInteger<RNG>(withMaximumWidth: Int, using: inout RNG
) -> BigUInt Create a big unsigned integer consisting of
width
uniformly distributed random bits.static func % (x: BigUInt, y: BigUInt
) -> BigUInt Divide
x
byy
and return the remainder.static func %= (x: inout BigUInt, y: BigUInt
) Divide
x
byy
and store the remainder inx
.static func &= (a: inout BigUInt, b: BigUInt
) Calculate the bitwise AND of
a
andb
and return the result.static func * (x: BigUInt, y: BigUInt
) -> BigUInt Multiply
a
byb
and return the result.static func *= (a: inout BigUInt, b: BigUInt
) Multiply
a
byb
and store the result ina
.static func + (a: BigUInt, b: BigUInt
) -> BigUInt Add
a
andb
together and return the result.static func += (a: inout BigUInt, b: BigUInt
) Add
a
andb
together, and store the sum ina
.static func - (a: BigUInt, b: BigUInt
) -> BigUInt Subtract
b
froma
and return the result.static func -= (a: inout BigUInt, b: BigUInt
) Subtract
b
froma
and store the result ina
.static func / (x: BigUInt, y: BigUInt
) -> BigUInt Divide
x
byy
and return the quotient.static func /= (x: inout BigUInt, y: BigUInt
) Divide
x
byy
and store the quotient inx
.static func < (a: BigUInt, b: BigUInt
) -> Bool Return true iff
a
is less thanb
.static func << <Other>(lhs: BigUInt, rhs: Other
) -> BigUInt static func <<= <Other>(lhs: inout BigUInt, rhs: Other
) static func == (a: BigUInt, b: BigUInt
) -> Bool Return true iff
a
is equal tob
.static func >> <Other>(lhs: BigUInt, rhs: Other
) -> BigUInt static func >>= <Other>(lhs: inout BigUInt, rhs: Other
) static func ^= (a: inout BigUInt, b: BigUInt
) Calculate the bitwise XOR of
a
andb
and return the result.static func |= (a: inout BigUInt, b: BigUInt
) Calculate the bitwise OR of
a
andb
, and store the result ina
.static func ~ (a: BigUInt
) -> BigUInt Return the ones’ complement of
a
.
Instance members
var bitWidth: Int
The minimum number of bits required to represent this integer in binary.
var description: String
Return the decimal representation of this integer.
var isZero: Bool
Return true iff this integer is zero.
var leadingZeroBitCount: Int
The number of leading zero bits in the binary representation of this integer in base
2^(Word.bitWidth)
. This is useful when you need to normalize aBigUInt
such that the top bit of its most significant word is 1.var playgroundDescription: Any
Return the playground quick look representation of this integer.
var trailingZeroBitCount: Int
The number of trailing zero bits in the binary representation of this integer.
var words: Words
subscript(bitAt: Int
) -> Bool func advanced(by: BigInt
) -> BigUInt Adds
n
toself
and returns the result. Traps if the result would be less than zero.func decrement(shiftedBy: Int
) Decrement this integer by one.
func distance(to: BigUInt
) -> BigInt Returns the (potentially negative) difference between
self
andother
as aBigInt
. Never traps.func encode(to: Encoder
) throws func greatestCommonDivisor(with: BigUInt
) -> BigUInt Returns the greatest common divisor of
self
andb
.func hash(into: inout Hasher
) Append this
BigUInt
to the specified hasher.func inverse(BigUInt
) -> BigUInt? Returns the multiplicative inverse of this integer in modulo
modulus
arithmetic, ornil
if there is no such number.func isPrime(rounds: Int
) -> Bool Returns true if this integer is probably prime. Returns false if this integer is definitely not prime.
func isStrongProbablePrime(BigUInt
) -> Bool Returns true iff this integer passes the strong probable prime test for the specified base.
func multiplied(by: BigUInt
) -> BigUInt Multiply this integer by
y
and return the result.func multiplied(byWord: Word
) -> BigUInt Multiply this big integer by a single Word, and return the result.
func multiply(byWord: Word
) Multiply this big integer by a single word, and store the result in place of the original big integer.
func multiplyAndAdd(BigUInt, Word, shiftedBy: Int
) Multiply
x
byy
, and add the result to this integer, optionally shiftedshift
words to the left.func power(Int
) -> BigUInt Returns this integer raised to the power
exponent
.func power(BigUInt, modulus: BigUInt
) -> BigUInt Returns the remainder of this integer raised to the power
exponent
in modulo arithmetic undermodulus
.func quotientAndRemainder(dividingBy: BigUInt
) -> (quotient: BigUInt, remainder: BigUInt) Divide this integer by
y
and return the resulting quotient and remainder.func serialize(
) -> Data Return a
Data
value that contains the base-256 representation of this integer, in network (big-endian) byte order.func signum(
) -> BigUInt Returns
1
if this value is, positive; otherwise,0
.func squareRoot(
) -> BigUInt Returns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater than
value
.func subtract(BigUInt, shiftedBy: Int
) Subtract
other
from this integer in place.other
is shiftedshift
digits to the left before being subtracted.func subtractReportingOverflow(BigUInt, shiftedBy: Int
) -> Bool Subtract
other
from this integer in place, and return a flag indicating if the operation caused an arithmetic overflow.other
is shiftedshift
digits to the left before being subtracted.func subtracting(BigUInt, shiftedBy: Int
) -> BigUInt Subtract
b
from this integer, and return the difference.b
is shiftedshift
digits to the left before being subtracted.func subtractingReportingOverflow(BigUInt
) -> (partialValue: BigUInt, overflow: Bool) Subtracts
other
fromself
, returning the result and a flag indicating arithmetic overflow.func subtractingReportingOverflow(BigUInt, shiftedBy: Int
) -> (partialValue: BigUInt, overflow: Bool) Subtract
other
from this integer, returning the difference and a flag indicating arithmetic overflow.other
is shiftedshift
digits to the left before being subtracted.
Type features
init(
) Creates a new value equal to zero.
init(extendedGraphemeClusterLiteral: Self.StringLiteralType
) static var isSigned: Bool
A Boolean value indicating whether this type is a signed integer type.
static var zero: Self
The zero value.
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.
static func != <Other>(lhs: Self, rhs: Other
) -> Bool Returns a Boolean value indicating whether the two given values are not equal.
static func != (lhs: Self, rhs: Self
) -> Bool static func & (lhs: Self, rhs: Self
) -> Self Returns the result of performing a bitwise AND operation on the two given values.
static func + (x: Self
) -> Self Returns the given number unchanged.
static func += (lhs: inout Self, rhs: Self
) static func -= (lhs: inout Self, rhs: Self
) static func ... (minimum: Self
) -> PartialRangeFrom<Self> Returns a partial range extending upward from a lower bound.
static func ... (maximum: Self
) -> PartialRangeThrough<Self> Returns a partial range up to, and including, its upper bound.
static func ... (minimum: Self, maximum: Self
) -> ClosedRange<Self> Returns a closed range that contains both of its bounds.
static func ..< (maximum: Self
) -> PartialRangeUpTo<Self> Returns a partial range up to, but not including, its upper bound.
static func ..< (minimum: Self, maximum: Self
) -> Range<Self> Returns a half-open range that contains its lower bound but not its upper bound.
static func < (x: Self, y: Self
) -> Bool static func < <Other>(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.
static func << <RHS>(lhs: Self, rhs: RHS
) -> Self Returns the result of shifting a value’s binary representation the specified number of digits to the left.
static 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.
static func <= <Other>(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.
static func <= (lhs: Self, rhs: Self
) -> Bool static func == (x: Self, y: Self
) -> Bool static func == <Other>(lhs: Self, rhs: Other
) -> Bool Returns a Boolean value indicating whether the two given values are equal.
static 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.
static func > <Other>(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.
static func > (lhs: Self, rhs: Self
) -> Bool static 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.
static func >= <Other>(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.
static func >= (lhs: Self, rhs: Self
) -> Bool static func >> <RHS>(lhs: Self, rhs: RHS
) -> Self Returns the result of shifting a value’s binary representation the specified number of digits to the right.
static func ^ (lhs: Self, rhs: Self
) -> Self Returns the result of performing a bitwise XOR operation on the two given values.
static func | (lhs: Self, rhs: Self
) -> Self Returns the result of performing a bitwise OR operation on the two given values.
Instance features
var magnitude: Self
The magnitude of this value.
func isMultiple(of: Self
) -> Bool