Dart - Using BigInt Examples

This tutorial shows you how to use BigInt class in Dart, which is used for arbitrary large number. This includes how to create a BigInt instance as well as the lists of available properties, methods, and operators.

Using int, the maximum value is limited at 9223372036854775807, which is the largest 64-bit signed integer. If you need to perform computation with a very big number bigger than it, you can use BigInt.

Creating BigInt Intances

The class doesn't have default constructor. The only available constructor is:

  BigInt.from(num value);

It only accepts number (primitive types). If you pass a double, it will be rounded down.

Examples:

  BigInt.from(5);
  BigInt.from(1.6); // Rounded down to 1

Dart also provides static properties that allow us to easily create a BigInt instance for these values:

  BigInt.zero
  BigInt.one
  BigInt.two

You can also use static method BigInt.parse

   external static BigInt parse(String source, {int radix});

Parameters:

  • String source: the value to be parsed which represents an integer literal. Optionally you can add '+' or '-' sign at the front (default is positive).
  • int radix (optional): set the base of the source. Default is 10, which is the base of decimal number.

If you want to parse a value represented by its binary, you need to set the radix to 2. If the value is hexadecimal, the radix should be set to 16 and you also need to remove the 0x prefix.

Unlike using from which rounded down

Examples:

  BigInt bi = BigInt.parse("9223372036854775807");
  BigInt bi = BigInt.parse("1000", radix: 2); // 8
  BigInt bi = BigInt.parse("FF", radix: 16); // 256

Using parse method, you have to pass a valid value to avoid FormatException being thrown. If you cannot guarantee that the value is always valid, you can use tryParse which has the same parameters and implementation as parse, except it returns null if the value is invalid.

  BigInt bi = BigInt.tryParse("1.1"); // null

Below are the list of properties, methods and operators of BigInt class.

Properties

Name Return Description
bitLength int Minimum bit required to store this instance
isEven bool Whether the value is even.
isNegative bool Whether the value is negative.
isOdd bool Whether the value is even.
isValildInt bool Whether it can be converted to integer without losing precision. It will return false if the value is larger than the maximum integer value (264)
sign int Sign of the value (-1 for negative, 0 for 0, 1 for positive).
hashCode int Hash code for the object.
runtimeType Type Runtime type for the object.

Method

Name Return Description
abs BigInt Returns the absolute value.
compareTo(BigInt other) int Compares the value with other. Returns -1 if less than other, 1 if greater than other, 0 if equals with other.
gcd(BigInt other) BigInt returns the GCD (Greatest Common Divisor) with other.
modInverse(BigInt modulus) BigInt Returns the modular multiplicative inverse of the value modulo modulus
modPow(BigInt exponent, BigInt modulus) BigInt Returns the value power of exponent modulo modulus
pow(int exponent) BigInt Returns value power by exponent.
remainder(BigInt other) BigInt Returns the remainder of division with other.
toDouble() double Converts to double.
toInt() int Converts to integer.
toRadixString(int radix) String Converts to string in the given radix.
toSigned(int width) BigInt Returns the least significant width bits of this integer, extending the highest retained bit to the sign.
toString()
String Returns the spring representation.
toUnsigned(int width) BigInt Returns the least significant width bits of this big integer as a non-negative number (i.e. unsigned representation).

Operators

Name Return Description
+ BigInt Addition
- BigInt Subtraction
* BigInt Multiplication
/ double Division
% BigInt Modulo
& BigInt Bit-wise and
<< BigInt Shift bits to left
>> double Shift bits to right
< bool Less than
<= bool Less than or equal
> bool Greater than
>=
bool Greater than or equal
^ BigInt Bit-wise xor
unary- BigInt Negative value
| BigInt Bit-wise or
~ BigInt Bit-wise negate
~/ BigInt Truncating

Below are some usage examples of the above operators:

  BigInt bi1 = BigInt.parse("100000000000000000000000000000000"); // 10 pow 32
  BigInt bi2 = BigInt.parse("200000000000000000000000000000000"); // 2 * (10 pow 32)
  print(bi1 + bi2); // 3 * (10 pow 32)
  print(bi1 - bi2); // -10 pow 32
  print(bi1 * bi2); // 2 * (10 pow 64)
  print(bi1 / bi2); // 0.5
  print(bi1 << 1); // 2 * (10 pow 32)
  print(bi1 >> 1); // 5 * (10 pow 31)
  print(bi1 & bi2); // 16165108075998070557326583005184
  print(bi1 | bi2); // 283834891924001929442673416994816
  print(bi1 ^ bi2); // 267669783848003858885346833989632
  print(bi1.pow(100)); // 10 pow 3200