Dart BigInt
last modified June 8, 2025
In Dart, BigInt is a data type that represents arbitrary-precision
integers, allowing developers to work with numbers larger than 64 bits. Unlike
standard int, which has a fixed size limit based on the system
architecture, BigInt can store extremely large values without
overflow, making it essential for applications requiring high-precision
calculations.
BigInt is particularly useful in domains where large numerical
values are common, such as:
- Cryptography - Secure encryption algorithms often require large prime numbers.
- Scientific computing - Simulating astronomical distances or molecular structures.
- Blockchain and hashing - Managing large cryptographic hashes and blockchain computations.
BigInt is part of Dart's core library, meaning it is available
without additional dependencies. It provides a rich set of mathematical
operations similar to regular integers, including:
The BigInt class provides methods for basic arithmetic, bitwise
operations, and modular arithmetic, allowing developers to handle large numbers
efficiently and accurately.
- Basic arithmetic: addition (+), subtraction (-), multiplication (*), division (~/)
- Bitwise operations: AND (&), OR (|), XOR (^), shifts (<<, >>)
- Modular arithmetic: modPow() for exponentiation, modInverse() for modular inversion
- Comparison methods: compareTo(), isEven, isOdd, isNegative
By leveraging BigInt, developers can perform precise
calculations without worrying about integer overflow, making it a crucial tool
for applications requiring high-precision numerical processing.
Creating BigInt Values
BigInt values can be created using the BigInt constructor or parse method. Literal syntax with the 'n' suffix is also available.
void main() {
var big1 = BigInt.from(123456789);
var big2 = BigInt.parse('98765432109876543210');
print(big1);
print(big2);
}
This shows two ways to create BigInt values. The first uses
BigInt.from to convert an int, while the second uses
BigInt.parse to convert a string representation of a large number.
$ dart main.dart 123456789 98765432109876543210
Basic Arithmetic Operations
BigInt supports all standard arithmetic operations like addition, subtraction, multiplication, and division. Division returns a truncated integer result.
void main() {
var a = BigInt.parse('12345678901234567890');
var b = BigInt.parse("987654321");
print('Addition: ${a + b}');
print('Subtraction: ${a - b}');
print('Multiplication: ${a * b}');
print('Division: ${a ~/ b}');
print('Remainder: ${a % b}');
}
We perform basic arithmetic on two large BigInt values. Note the use of ~/ for integer division. Regular / would require double conversion.
$ dart main.dart Addition: 12345678902222222211 Subtraction: 12345678900246913569 Multiplication: 12193263112482853211126352690 Division: 12499999887 Remainder: 339506163
Comparison and Properties
BigInt values can be compared using standard operators. They also provide properties like isEven, isOdd, and sign.
void main() {
var x = BigInt.parse('12345678901234567890');
var y = BigInt.parse('98765432109876543210');
print('x < y: ${x < y}');
print('x == y: ${x == y}');
print('x.isEven: ${x.isEven}');
print('y.isOdd: ${y.isOdd}');
print('x.sign: ${x.sign}');
print('y.bitLength: ${y.bitLength}');
}
We compare two BigInt values and check their properties.
bitLength returns the minimum number of bits needed to store the
number.
$ dart main.dart x < y: true x == y: false x.isEven: true y.isOdd: false x.sign: 1 y.bitLength: 67
Bitwise Operations
BigInt supports bitwise operations like AND, OR, XOR, and shifts. These are useful for low-level programming and cryptography.
void main() {
var a = BigInt.parse('0xFF00FF00FF00FF00FF');
var b = BigInt.parse('0x00FF00FF00FF00FF00');
print('AND: ${a & b}');
print('OR: ${a | b}');
print('XOR: ${a ^ b}');
print('NOT a: ${~a}');
print('Shift left: ${a << 4}');
print('Shift right: ${a >> 8}');
}
We perform various bitwise operations on hexadecimal BigInt values.
The results show how each operation affects the binary representation.
$ dart main.dart AND: 0 OR: 4722366482869645213695 XOR: 4722366482869645213695 NOT a: -4703991516010230251776 Shift left: 75263864256163684028400 Shift right: 18374966859414961920
Modular Arithmetic
BigInt provides methods for modular arithmetic, including pow and
modPow which are essential for cryptographic algorithms.
void main() {
var base = BigInt.from(5);
var exponent = BigInt.from(100);
var modulus = BigInt.from(101);
print('5^100: ${base.pow(100)}');
print('5^100 mod 101: ${base.modPow(exponent, modulus)}');
print('Modular inverse of 5 mod 101: ${base.modInverse(modulus)}');
}
We demonstrate exponentiation and modular operations. modPow
efficiently computes large exponents under modulus. modInverse
finds the modular multiplicative inverse.
$ dart main.dart 5^100: 7888609052210118054117285652827862296732064351090230047702789306640625 5^100 mod 101: 1 Modular inverse of 5 mod 101: 81
Best Practices
- Performance: Use regular int when possible as
BigIntis slower. - Memory:
BigIntconsumes more memory than regular integers. - Conversion: Explicitly convert between int and
BigIntwhen needed. - Error Handling: Handle potential FormatException when parsing.
Source
This tutorial covered Dart's BigInt with practical examples
demonstrating its key features and usage patterns for arbitrary-precision
arithmetic.
Author
List all Dart tutorials.