BInt
The BInt structure
struct BInt
Overview
Please, see About BInt
Contstants
static let ZERO: BInt
BInt(0)
static let ONE: BInt
BInt(1)
static let TWO: BInt
BInt(2)
static let THREE: BInt
BInt(3)
static let FOUR: BInt
BInt(4)
static let FIVE: BInt
BInt(5)
static let SIX: BInt
BInt(6)
static let SEVEN: BInt
BInt(7)
static let EIGHT: BInt
BInt(8)
static let NINE: BInt
BInt(9)
static let TEN: BInt
BInt(10)
Properties
var abs: BInt
The absolute value of
self
var bitWidth: Int
The number of bits in the binary representation of the magnitude of
self
. 0 ifself
= 0var description: String
Base 10 string value of
self
var isEven: Bool
Is
true
ifself
is even,false
ifself
is oddvar isNegative: Bool
The sign,
true
ifself
< 0,false
ifself
>= 0var isNotZero: Bool
Is
false
ifself
= 0,true
otherwisevar isOdd: Bool
Is
true
ifself
is odd,false
ifself
is evenvar isOne: Bool
Is
true
ifself
= 1,false
otherwisevar isPositive: Bool
Is
true
ifself
> 0,false
otherwisevar isPow2: Bool
Is
true
ifself
is a power of 2: 1, 2, 4, 8 …,false
otherwisevar isZero: Bool
Is
true
ifself
= 0,false
otherwisevar leadingZeroBitCount: Int
The number of leading zero bits in the magnitude of
self
. 0 ifself
= 0var magnitude: BInt
var population: Int
The number of 1 bits in the magnitude of
self
signum
var trailingZeroBitCount: Int
The number of trailing zero bits in the magnitude of
self
. 0 ifself
= 0
Constructors
init(Limbs, Bool
) Constructs a
BInt
from magnitude and signinit(Int
) Constructs a
BInt
from anInt
valueinit?(Double
) Constructs a
BInt
from a decimal value,nil
ifd
is infinite or NaNinit?(String, radix: Int
) Constructs a
BInt
from a String value and radix,nil
ifx
does not designate an integer in the given radixinit(bitWidth: Int
) Constructs a uniformly distributed random
BInt
in the range 0 ..< 2 ^bitWidth
init(magnitude: Bytes
) Constructs a
BInt
from a big-endian magnitude byte arrayinit(signed: Bytes
) Constructs a
BInt
from a big-endian 2’s complement byte array
Conversion
func asDouble(
) -> Double self
as aDouble
func asInt(
) -> Int? self
as anInt
func asMagnitudeBytes(
) -> Bytes Byte array representation of magnitude value
func asSignedBytes(
) -> Bytes Byte array representation of 2’s complement value
func asString(radix: Int, uppercase: Bool
) -> String self
as aString
with a given radix
Addition
+(_:)
static func + (x: BInt, y: BInt
) -> BInt Addition
static func + (x: BInt, y: Int
) -> BInt Addition
static func + (x: Int, y: BInt
) -> BInt Addition
static func += (x: inout BInt, y: BInt
) x = x + y
static func += (x: inout BInt, y: Int
) x = x + y
Subtraction
static func - (x: BInt, y: BInt
) -> BInt Subtraction
static func - (x: BInt, y: Int
) -> BInt Subtraction
static func - (x: Int, y: BInt
) -> BInt Subtraction
static func -= (x: inout BInt, y: BInt
) x = x - y
static func -= (x: inout BInt, y: Int
) x = x - y
Negation
-(_:)
negate()
Multiplication
static func * (x: BInt, y: BInt
) -> BInt Multiplication
static func * (x: BInt, y: Int
) -> BInt Multiplication
static func * (x: Int, y: BInt
) -> BInt Multiplication
static func *= (x: inout BInt, y: BInt
) x = x * y
static func *= (x: inout BInt, y: Int
) x = x * y
Division
/(_:_:)-m4w0
/(_:_:)-965hy
/(_:_:)-49klv
/=(_:_:)-2y3x4
/=(_:_:)-5xeas
func quotientAndRemainder(dividingBy: BInt
) -> (quotient: BInt, remainder: BInt) Division
func quotientAndRemainder(dividingBy: BInt, inout BInt, inout BInt
) Division
func quotientAndRemainder(dividingBy: Int
) -> (quotient: BInt, remainder: Int) Division
func quotientAndRemainder(dividingBy: Int, inout BInt, inout Int
) Division
func quotientExact(dividingBy: BInt
) -> BInt Exact division - that is, the remainder of the division is known to be 0
Exponentiation
static func ** (a: BInt, x: Int
) -> BInt Exponentiation
func expMod(BInt, BInt
) -> BInt Modular exponentiation - BInt version
func expMod(BInt, Int
) -> Int Modular exponentiation - Int version
Remainder and Modulus
static func % (x: BInt, y: BInt
) -> BInt Remainder
static func % (x: BInt, y: Int
) -> BInt Remainder
static func % (x: Int, y: BInt
) -> BInt Remainder
static func %= (x: inout BInt, y: BInt
) x = x % y
static func %= (x: inout BInt, y: Int
) x = x % y
func mod(BInt
) -> BInt Modulus - BInt version
func mod(Int
) -> Int Modulus - Int version
func modInverse(BInt
) -> BInt Inverse modulus - BInt version
func modInverse(Int
) -> Int Inverse modulus - Int version
GCD and LCM
func gcd(BInt
) -> BInt Greatest common divisor - BInt version
func gcd(Int
) -> BInt Greatest common divisor - Int version
func gcdExtended(BInt
) -> (g: BInt, a: BInt, b: BInt) Extended greatest common divisor - BInt version
func gcdExtended(Int
) -> (g: BInt, a: BInt, b: BInt) Extended greatest common divisor - Int version
func lcm(BInt
) -> BInt Least common multiple - BInt version
func lcm(Int
) -> BInt Least common multiple - Int version
Comparison
static func == (x: BInt, y: BInt
) -> Bool Equal
static func == (x: BInt, y: Int
) -> Bool Equal
static func == (x: Int, y: BInt
) -> Bool Equal
static func != (x: BInt, y: BInt
) -> Bool Not equal
static func != (x: BInt, y: Int
) -> Bool Not equal
static func != (x: Int, y: BInt
) -> Bool Not equal
static func < (x: BInt, y: BInt
) -> Bool Less than
static func < (x: BInt, y: Int
) -> Bool Less than
static func < (x: Int, y: BInt
) -> Bool Less than
static func > (x: BInt, y: BInt
) -> Bool Greater than
static func > (x: BInt, y: Int
) -> Bool Greater than
static func > (x: Int, y: BInt
) -> Bool Greater than
static func <= (x: BInt, y: BInt
) -> Bool Less than or equal
static func <= (x: BInt, y: Int
) -> Bool Less than or equal
static func <= (x: Int, y: BInt
) -> Bool Less than or equal
static func >= (x: BInt, y: BInt
) -> Bool Greater than or equal
static func >= (x: BInt, y: Int
) -> Bool Greater than or equal
static func >= (x: Int, y: BInt
) -> Bool Greater than or equal
Shift Operations
<<(_:_:)
static func <<= (x: inout BInt, n: Int
) x = x << n
static func <<= <RHS>(lhs: inout BInt, rhs: RHS
) >>(_:_:)
static func >>= (x: inout BInt, n: Int
) x = x >> n
static func >>= <RHS>(lhs: inout BInt, rhs: RHS
)
Bit Operations
&(_:_:)
static func &= (x: inout BInt, y: BInt
) x = x & y
|(_:_:)
static func |= (x: inout BInt, y: BInt
) x = x | y
^(_:_:)
static func ^= (x: inout BInt, y: BInt
) x = x ^ y
static func ~ (x: BInt
) -> BInt Bitwise
not
operator - behaves as if two’s complement arithmetic were usedfunc clearBit(Int
) Clear a specified bit - a no-op if bit number < 0
func flipBit(Int
) Invert a specified bit - a no-op if bit number < 0
func setBit(Int
) Set a specified bit - a no-op if bit number < 0
func testBit(Int
) -> Bool Test a specified bit -
false
if bit number < 0
Root Extraction
func sqrt(
) -> BInt Square root of a non-negative number
func sqrtRemainder(
) -> (root: BInt, rem: BInt) Square root and remainder of a non-negative number
func isPerfectSquare(
) -> Bool Check whether
self
is a perfect square, that is, for some integerx
,self
=x^2
func sqrtMod(BInt
) -> BInt? Square root modulo a prime number - BInt version
func sqrtMod(Int
) -> Int? Square root modulo a prime number - Int version
func root(Int
) -> BInt n’th root
func rootRemainder(Int
) -> (root: BInt, rem: BInt) n’th root and remainder
func isPerfectRoot(
) -> Bool Check whether
self
is a perfect root, that is, for some integerx
andn
> 1self
=x^n
Prime Number
static func probablePrime(Int, Int
) -> BInt A probable prime number with a given bitwidth
func nextPrime(Int
) -> BInt The next probable prime greater than
self
func isProbablyPrime(Int
) -> Bool Checks whether
self
is prime using the Miller-Rabin algorithmstatic func primorial(Int
) -> BInt Product of primes up to n
Miscellaneous
func randomLessThan(
) -> BInt Random value
func randomFrom(BInt
) -> BInt Random value
func randomTo(BInt
) -> BInt Random value
static func binomial(Int, Int
) -> BInt Compute binomial coefficient
n
overk
static func factorial(Int
) -> BInt Factorial function
static func fibonacci(Int
) -> BInt n’th Fibonacci number
static func fibonacci2(Int
) -> (BInt, BInt) Fibonacci pair, n’th and n’th + 1 Fibonacci number
func jacobiSymbol(BInt
) -> Int Jacobi symbol - BInt version. If m is an odd prime, this is also the Legendre symbol
func jacobiSymbol(Int
) -> Int Jacobi symbol - Int version. If
m
is an odd prime, this is also the Legendre symbolfunc kroneckerSymbol(BInt
) -> Int Kronecker symbol - BInt version. If
m
is positive and odd, this is also the Jacobi symbolfunc kroneckerSymbol(Int
) -> Int Kronecker symbol - Int version. If
m
is positive and odd, this is also the Jacobi symbolstatic func lucas(Int
) -> BInt n’th Lucas number
static func lucas2(Int
) -> (BInt, BInt) Lucas pair, n’th and n’th + 1 Lucas number