Skip to main content
BigInt is a class in the dart:core library representing an arbitrarily large signed integer. Unlike the primitive int type, which is platform-dependent (fixed 64-bit on native, IEEE 754 double-precision on the web), BigInt provides platform-independent precision limited only by available memory.

Instantiation

BigInt instances are immutable objects created via static methods or factories.

Parsing Strings

Parsing string representations is the primary method for creating large integers to avoid the precision loss inherent in numeric literals.
// Decimal parsing
BigInt decimal = BigInt.parse('123456789012345678901234567890');

// Hexadecimal parsing (radix 16)
// Note: The string must not contain the '0x' prefix.
BigInt hex = BigInt.parse('1FFFFFFFFFFFFF', radix: 16);

Converting Numbers

Existing int or double values can be converted to BigInt using BigInt.from. Behavior: The num passed to BigInt.from is converted to an integer. If the input is a double with a fractional part, the value is truncated (rounded towards zero).
// From integer
BigInt fromInt = BigInt.from(100);

// From double (Truncates fractional part)
BigInt fromDouble = BigInt.from(12345.6789); // Result: 12345

Constants

The class provides static constants to minimize allocation overhead for common values.
BigInt zero = BigInt.zero;
BigInt one = BigInt.one;
BigInt two = BigInt.two;

Arithmetic Operations

BigInt implements standard arithmetic operators via operator overloading, with the exception of the standard division operator (/). Note: The BigInt class does not define the standard division operator (/). Attempting to use it results in a compilation error. Division must be performed using the truncating integer division operator (~/).
BigInt a = BigInt.parse('1000000000000000000');
BigInt b = BigInt.from(2);

// Addition (Returns BigInt)
BigInt sum = a + b;

// Multiplication (Returns BigInt)
BigInt product = a * b;

// Integer Division (Truncating, returns BigInt)
BigInt intQuotient = a ~/ b; 

// Modulo (Returns BigInt)
BigInt remainder = a % b;

// Unary Negation (Returns BigInt)
BigInt negative = -a;

Bitwise Operations

Bitwise operators treat the integer as a binary sequence in two’s complement representation.
BigInt val = BigInt.parse('1024');

// Left shift
BigInt shiftedLeft = val << 1; 

// Right shift
BigInt shiftedRight = val >> 1;

// Bitwise AND, OR, XOR
BigInt bitwiseAnd = val & BigInt.from(255);
BigInt bitwiseOr = val | BigInt.from(1);
BigInt bitwiseXor = val ^ BigInt.from(1024);

// Bitwise NOT (Unary prefix)
BigInt inverted = ~val;

Comparison

BigInt implements the Comparable interface. Comparison operators (<, >, <=, >=) function identically to standard integers.
BigInt x = BigInt.from(10);
BigInt y = BigInt.from(20);

bool isLess = x < y; // true
int comparison = x.compareTo(y); // Returns -1, 0, or 1

Type Conversion

Converting BigInt back to primitive types requires explicit method calls.

Conversion to String

The toString method returns the decimal representation. toRadixString allows specification of the base (2 through 36).
BigInt massive = BigInt.parse('99999999999999999999999');
String hexStr = massive.toRadixString(16);

Conversion to int

The toInt() method converts the BigInt to a primitive int. The behavior differs by platform if the value exceeds the platform’s int range:
  1. Native (VM): The result is the least significant 64 bits of the value. This behaves like standard integer overflow (wrapping).
  2. Web (JavaScript): The result is converted to a JavaScript Number (double). This allows values larger than 2532^{53}, but precision is lost for values exceeding the safe integer range. If the value exceeds double.maxFinite, the result is double.infinity or double.negativeInfinity.
Use isValidInt to determine if the conversion can occur without loss of precision or overflow.
BigInt massive = BigInt.parse('99999999999999999999999');

if (massive.isValidInt) {
  int exactVal = massive.toInt();
} else {
  // On VM: Returns a wrapped value (least significant 64 bits)
  // On Web: Returns approx 1.0e+23 (precision loss)
  print("Value does not fit in platform int");
}

Conversion to double

The toDouble() method converts the value to a floating-point number. This may result in a loss of precision for very large values due to the limitations of IEEE 754 representation.
double doubleVal = massive.toDouble();

Memory and Performance

  • Heap Allocation: Unlike int, which is often stack-allocated or handled as a tagged pointer, BigInt is always an object allocated on the heap.
  • Immutability: Operations on BigInt produce new instances; the original object is never modified.
  • Complexity: Arithmetic operations are significantly slower than primitive int operations due to the overhead of arbitrary-precision algorithms. Complexity generally scales with the number of bits in the operand.
Master Dart with Deep Grasping Methodology!Learn More