Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

BigInt is a built-in Dart class representing an arbitrarily large integer. Unlike the standard int type, which is constrained to a 64-bit two’s complement representation (ranging from -2^63 to 2^63 - 1), BigInt can store integer values of any size, limited only by the available system memory.

Instantiation

Because Dart’s numeric literals are parsed as 64-bit integers by the compiler, instantiating a BigInt that exceeds the 64-bit limit requires parsing from a String. Using BigInt.from() with a numeric literal that exceeds the 64-bit limit results in a compile-time error.
// Recommended: Parsing from a string prevents 64-bit overflow errors
BigInt largeBase10 = BigInt.parse('922337203685477580899999');

// Automatically detects and parses hexadecimal if prefixed with '0x'
BigInt largeHex = BigInt.parse('0x1fffffffffffff123456789');

// Parsing without a prefix requires explicitly specifying the radix
BigInt explicitHex = BigInt.parse('1fffffffffffff123456789', radix: 16);
BigInt binaryValue = BigInt.parse('101010', radix: 2);

// Converting from an existing 64-bit int or double
BigInt fromNativeInt = BigInt.from(42);

// Pre-defined constants
BigInt zero = BigInt.zero;
BigInt one = BigInt.one;
BigInt two = BigInt.two;

Operators

BigInt overloads standard arithmetic, relational, and bitwise operators. This allows instances to be manipulated using the same syntax as native int types.
BigInt a = BigInt.parse('100000000000000000000');
BigInt b = BigInt.from(5);

// Arithmetic
BigInt sum = a + b;
BigInt difference = a - b;
BigInt product = a * b;
BigInt quotient = a ~/ b;     // Truncating integer division, returns BigInt
double floatQuotient = a / b; // Standard division, returns double
BigInt modulo = a % b;

// Bitwise
BigInt shifted = a << 2;
BigInt bitwiseAnd = a & b;

// Relational
bool isGreater = a > b;
Note: The standard division operator (/) is fully supported between BigInt instances, but it evaluates to a double. To maintain arbitrary precision and return a BigInt, use the truncating division operator (~/).

Key Properties and Methods

BigInt provides several properties and methods for mathematical operations and state inspection.
BigInt val = BigInt.parse('256');

// Inspection
bool isEven = val.isEven;
bool isNegative = val.isNegative;
int sign = val.sign;      // Returns -1, 0, or 1
int bits = val.bitLength; // Minimum number of bits required to store the value

// Mathematical operations
BigInt power = val.pow(3);
BigInt greatestCommonDivisor = val.gcd(BigInt.from(128));

// Downcasting
int nativeInt = val.toInt();          // Truncates if value exceeds 64 bits
double nativeDouble = val.toDouble(); // Loses precision if value exceeds 53 bits

Technical Characteristics

  • Immutability: Instances of BigInt are strictly immutable. All arithmetic and bitwise operations allocate and return a new BigInt instance in memory.
  • Performance Overhead: Because BigInt utilizes software-based arbitrary-precision arithmetic rather than hardware-level CPU instructions, operations are significantly slower and consume more memory than native int operations.
  • Web Compilation: When compiled to the web via dart2js or dartdevc (DDC), Dart’s BigInt uses a custom Dart implementation (a class backed by arrays of smaller integers) to guarantee consistent semantics with the Dart VM. It does not map to the native JavaScript BigInt type. Native JS BigInts are instead accessed via JS interop (e.g., JSBigInt).
Master Dart with Deep Grasping Methodology!Learn More