BInt - p2p-org/solana-swift GitHub Wiki
BInt
BInt is an arbitrary precision integer value type. It stores a number in base 2^64 notation as an array. Each element of the array is called a limb, which is of type UInt64, the whole array is called limbs and has the type [UInt64]. A boolean sign variable determines if the number is positive or negative. If sign == true, then the number is smaller than 0, otherwise it is greater or equal to 0. It stores the 64 bit digits in little endian, that is, the least significant digit is stored in the array index 0:
public struct BInt:
SignedNumeric, // Implies Numeric, Equatable, ExpressibleByIntegerLiteral
BinaryInteger, // Implies Hashable, CustomStringConvertible, Strideable, Comparable
ExpressibleByFloatLiteral
limbs == [] := undefined, should throw an error
limbs == [0], sign == false := 0, defined as positive
limbs == [0], sign == true := undefined, should throw an error
limbs == [n] := n if sign == false, otherwise -n, given 0 <= n < 2^64
limbs == [l0, l1, l2, ..., ln] :=
(l0 * 2^(0*64)) +
(11 * 2^(1*64)) +
(12 * 2^(2*64)) +
... +
(ln * 2^(n*64))
Inheritance
// Implies Hashable, CustomStringConvertible, Strideable, Comparable ExpressibleByFloatLiteral
, // Implies Numeric, Equatable, ExpressibleByIntegerLiteral BinaryInteger
, SignedNumeric
Nested Type Aliases
Magnitude
public typealias Magnitude = UInt64
Words
public typealias Words = [UInt]
Initializers
init(hex:)
Initialise a BInt from a hexadecimal string
public init(hex: String)
Parameters
- hex: the hexadecimal string to convert to a big integer
init(_:)
Initialise from an unsigned, 64 bit integer
public init(_ n: UInt64)
Parameters
- n: the 64 bit unsigned integer to convert to a BInt
init(data:)
Initialise from big-endian data
public init(data: Data)
Parameters
- data: the data to convert to a Bignum
init(_:)
Create an instance initialized to a string value.
public init(_ str: String)
init(floatLiteral:)
public init(floatLiteral value: Double)
init(integerLiteral:)
public init(integerLiteral value: Int)
init?(exactly:)
public init?<T>(exactly source: T) where T : BinaryInteger
init(_:)
Creates an integer from the given floating-point value, rounding toward zero.
public init<T>(_ source: T) where T : BinaryFloatingPoint
init(_:)
Creates a new instance from the given integer.
public init<T>(_ source: T) where T : BinaryInteger
init(clamping:)
Creates a new instance with the representable value that’s closest to the given integer.
public init<T>(clamping source: T) where T : BinaryInteger
init?(exactly:)
Creates an integer from the given floating-point value, if it can be represented exactly.
public init?<T>(exactly source: T) where T : BinaryFloatingPoint
init(truncatingIfNeeded:)
Creates a new instance from the bit pattern of the given instance by sign-extending or truncating to fit this type.
public init<T>(truncatingIfNeeded source: T) where T : BinaryInteger
init(number:withBase:)
public init(number: String, withBase base: Int)
Properties
dec
Decimal string representation
public var dec: String
hex
Hexadecimal string representation
public var hex: String
magnitude
public var magnitude: UInt64
words
A collection containing the words of this value’s binary representation, in order from the least significant to most significant.
public var words: BInt.Words
description
public var description: String
isSigned
A Boolean value indicating whether this type is a signed integer type.
public static var isSigned: Bool
bitWidth
The number of bits in the current binary representation of this value.
public var bitWidth: Int
trailingZeroBitCount
The number of trailing zeros in this value’s binary representation.
public var trailingZeroBitCount: Int
Methods
asString(withBase:)
public func asString(withBase base: Int) -> String
hash(into:)
public func hash(into hasher: inout Hasher)
signum()
Returns -1 if this value is negative and 1 if it’s positive; otherwise, 0.
public func signum() -> BInt
negate()
public mutating func negate()
quotientAndRemainder(dividingBy:)
Returns the quotient and remainder of this value divided by the given value.
public func quotientAndRemainder(dividingBy rhs: BInt) -> (quotient: BInt, remainder: BInt)
Operators
<<
public static func <<<T: BinaryInteger>(lhs: BInt, rhs: T) -> BInt
<<=
public static func <<=<T: BinaryInteger>(lhs: inout BInt, rhs: T)
>>
public static func >><T: BinaryInteger>(lhs: BInt, rhs: T) -> BInt
>>=
public static func >>=<T: BinaryInteger>(lhs: inout BInt, rhs: T)
&
Returns the result of performing a bitwise AND operation on the two given values.
public static func &(lhs: BInt, rhs: BInt) -> BInt
&=
Stores the result of performing a bitwise AND operation on the two given values in the left-hand-side variable.
public static func &=(lhs: inout BInt, rhs: BInt)
|
public static func |(lhs: BInt, rhs: BInt) -> BInt
|=
public static func |=(lhs: inout BInt, rhs: BInt)
^
public static func ^(lhs: BInt, rhs: BInt) -> BInt
^=
public static func ^=(lhs: inout BInt, rhs: BInt)
~
public prefix static func ~(x: BInt) -> BInt
+=
public static func +=(lhs: inout BInt, rhs: BInt)
+
public static func +(lhs: BInt, rhs: BInt) -> BInt
-
public static prefix func -(n: BInt) -> BInt
-
public static func -(lhs: BInt, rhs: BInt) -> BInt
-=
public static func -=(lhs: inout BInt, rhs: BInt)
*
public static func *(lhs: BInt, rhs: BInt) -> BInt
*=
public static func *=(lhs: inout BInt, rhs: BInt)
**
public static func **(lhs: BInt, rhs: Int) -> BInt
/
public static func /(lhs: BInt, rhs:BInt) -> BInt
/=
public static func /=(lhs: inout BInt, rhs: BInt)
%
public static func %(lhs: BInt, rhs: BInt) -> BInt
%=
public static func %=(lhs: inout BInt, rhs: BInt)
==
public static func ==(lhs: BInt, rhs: BInt) -> Bool
<
public static func <(lhs: BInt, rhs: BInt) -> Bool
>
public static func >(lhs: BInt, rhs: BInt) -> Bool
<=
public static func <=(lhs: BInt, rhs: BInt) -> Bool
>=
public static func >=(lhs: BInt, rhs: BInt) -> Bool