A BigInt can represent arbitrarily large integers.
It is implemented under the hood with GMP.
NOTE To use BigInt, you must explicitly import it with require "big"
Returns a number for given digits and base.
Creates a BigInt with the value denoted by str in the given base.
Creates a BigInt from the given num.
Returns num.
Creates a BigInt with the value zero.
Returns self modulo other.
Returns the value of raising self to the power of exponent.
Returns the result of shifting this number's bits count positions to the left.
Returns the result of shifting this number's bits count positions to the right.
Returns the absolute value of this number.
Returns the number of bits of this int value.
Returns the digits of a number in a given base.
Returns the greatest common divisor of self and other.
Returns the greatest common divisor of self and other.
Returns the least common multiple of self and other.
Returns the least common multiple of self and other.
Counts 1-bits in the binary representation of this integer.
Returns self remainder other.
Divides self by other using truncated division.
Converts self to BigDecimal.
Returns a BigInt representing this integer.
Returns a BigRational representing this integer.
Appends a string representation of this integer to the given io.
Returns a string representation of this integer.
Returns the number of trailing 0-bits.
Comparable(Float)
Comparable(BigInt)
Comparable(UInt128 | UInt16 | UInt32 | UInt64 | UInt8)
Comparable(Int128 | Int16 | Int32 | Int64 | Int8)
Int
Int
Comparable(BigDecimal)
Comparable(BigRational)
Comparable(BigInt)
Number
Number
Number
Number
Comparable(BigFloat)
Steppable
Comparable(Number)
Value
Object
Object
Object
Returns a number for given digits and base. The digits are expected as an Enumerable with the least significant digit as the first element.
base must not be less than 2.
All digits must be within 0...base.
Creates a BigInt with the value denoted by str in the given base.
Raises ArgumentError if the string doesn't denote a valid integer.
require "big"
BigInt.new("123456789123456789123456789123456789") # => 123456789123456789123456789123456789
BigInt.new("123_456_789_123_456_789_123_456_789") # => 123456789123456789123456789
BigInt.new("1234567890ABCDEF", base: 16) # => 1311768467294899695 Creates a BigInt from the given num.
Returns a read-only Slice of the limbs that make up this integer, which is effectively abs.digits(2 ** N) where N is the number of bits in LibGMP::MpLimb, except that an empty Slice is returned for zero.
This assumes GMP wasn't built with its experimental nails support: https://gmplib.org/manual/Low_002dlevel-Functions
num must be finite.
Returns a read-only Slice of the limbs that make up this integer, which is effectively abs.digits(2 ** N) where N is the number of bits in LibGMP::MpLimb, except that an empty Slice is returned for zero.
This assumes GMP wasn't built with its experimental nails support: https://gmplib.org/manual/Low_002dlevel-Functions
Returns a read-only Slice of the limbs that make up this integer, which is effectively abs.digits(2 ** N) where N is the number of bits in LibGMP::MpLimb, except that an empty Slice is returned for zero.
This assumes GMP wasn't built with its experimental nails support: https://gmplib.org/manual/Low_002dlevel-Functions
Returns a read-only Slice of the limbs that make up this integer, which is effectively abs.digits(2 ** N) where N is the number of bits in LibGMP::MpLimb, except that an empty Slice is returned for zero.
This assumes GMP wasn't built with its experimental nails support: https://gmplib.org/manual/Low_002dlevel-Functions
Returns the value of raising self to the power of exponent.
Raises ArgumentError if exponent is negative: if this is needed, either use a float base or a float exponent.
Raises OverflowError in case of overflow.
2 ** 3 # => 8 2 ** 0 # => 1 2 ** -1 # ArgumentError
Returns the result of shifting this number's bits count positions to the left.
8000 << 1 # => 16000 8000 << 2 # => 32000 8000 << 32 # => 0 8000 << -1 # => 4000
Returns the result of shifting this number's bits count positions to the right. Also known as arithmetic right shift.
8000 >> 1 # => 4000 8000 >> 2 # => 2000 8000 >> 32 # => 0 8000 >> -1 # => 16000 -8000 >> 1 # => -4000
Returns the absolute value of this number.
123.abs # => 123 -123.abs # => 123
Returns the number of bits of this int value.
“The number of bits” means that the bit position of the highest bit which is different to the sign bit. (The bit position of the bit 2**n is n+1.) If there is no such bit (zero or minus one), zero is returned.
I.e. This method returns ceil(log2(self < 0 ? -self : self + 1)).
0.bit_length # => 0 1.bit_length # => 1 2.bit_length # => 2 3.bit_length # => 2 4.bit_length # => 3 5.bit_length # => 3 # The above is the same as 0b0.bit_length # => 0 0b1.bit_length # => 1 0b10.bit_length # => 2 0b11.bit_length # => 2 0b100.bit_length # => 3 0b101.bit_length # => 3
Returns the digits of a number in a given base. The digits are returned as an array with the least significant digit as the first array element.
12345.digits # => [5, 4, 3, 2, 1] 12345.digits(7) # => [4, 6, 6, 0, 5] 12345.digits(100) # => [45, 23, 1] -12345.digits(7) # => ArgumentError
Counts 1-bits in the binary representation of this integer.
5.popcount # => 2 -15.popcount # => 29
Divides self by other using truncated division.
In truncated division, given two integers x and y:
q = x.tdiv(y) is rounded toward zeror = x.remainder(y) has the sign of xx == q*y + rFor example:
x y x / y x % y 5 3 1 2 -5 3 -1 -2 5 -3 -1 2 -5 -3 1 -2
Raises if other is 0, or if other is -1 and self is signed and is the minimum value for that integer type.
Converts self to BigDecimal.
require "big" 123456789012345678.to_big_d
Returns a BigRational representing this integer.
require "big" 123.to_big_r
Appends a string representation of this integer to the given io.
base specifies the radix of the written string, and must be either 62 or a number between 2 and 36. By default, digits above 9 are represented by ASCII lowercase letters (a for 10, b for 11, etc.), but uppercase letters may be used if upcase is true, unless base 62 is used. In that case, lowercase letters are used for 10 to 35, and uppercase ones for 36 to 61, and upcase must be false.
precision specifies the minimum number of digits in the written string. If there are fewer digits than this number, the string is left-padded by zeros. If self and precision are both zero, returns an empty string.
Returns a string representation of this integer.
base specifies the radix of the returned string, and must be either 62 or a number between 2 and 36. By default, digits above 9 are represented by ASCII lowercase letters (a for 10, b for 11, etc.), but uppercase letters may be used if upcase is true, unless base 62 is used. In that case, lowercase letters are used for 10 to 35, and uppercase ones for 36 to 61, and upcase must be false.
precision specifies the minimum number of digits in the returned string. If there are fewer digits than this number, the string is left-padded by zeros. If self and precision are both zero, returns an empty string.
1234.to_s # => "1234" 1234.to_s(2) # => "10011010010" 1234.to_s(16) # => "4d2" 1234.to_s(16, upcase: true) # => "4D2" 1234.to_s(36) # => "ya" 1234.to_s(62) # => "jU" 1234.to_s(precision: 2) # => "1234" 1234.to_s(precision: 6) # => "001234"
Returns the number of trailing 0-bits.
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/BigInt.html