ZetCode

Dart BigInt

last modified June 4, 2025

In Dart, BigInt represents arbitrary-precision integers. Unlike regular integers, BigInt can store values larger than 64 bits. This is useful for cryptography, financial calculations, and other domains needing large numbers.

BigInt is part of Dart's core library and provides mathematical operations similar to regular integers. It supports all basic arithmetic operations and bitwise operations.

Creating BigInt Values

BigInt values can be created using the BigInt constructor or parse method. Literal syntax with the 'n' suffix is also available.

main.dart
void main() {
  // Using constructor
  var big1 = BigInt.from(123456789);
  
  // Using parse
  var big2 = BigInt.parse('98765432109876543210');
  
  // Using literal
  var big3 = 12345678901234567890n;
  
  print(big1);
  print(big2);
  print(big3);
}

This shows three ways to create BigInt values. The parse method handles very large numbers as strings. The 'n' suffix creates BigInt literals directly.

$ dart main.dart
123456789
98765432109876543210
12345678901234567890

Basic Arithmetic Operations

BigInt supports all standard arithmetic operations like addition, subtraction, multiplication, and division. Division returns a truncated integer result.

main.dart
void main() {
  var a = 12345678901234567890n;
  var b = 987654321n;
  
  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: 12193263113702179522374638010
Division: 12499999873
Remainder: 370370367

Comparison and Properties

BigInt values can be compared using standard operators. They also provide properties like isEven, isOdd, and sign.

main.dart
void main() {
  var x = 12345678901234567890n;
  var y = 98765432109876543210n;
  
  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: 66

Bitwise Operations

BigInt supports bitwise operations like AND, OR, XOR, and shifts. These are useful for low-level programming and cryptography.

main.dart
void main() {
  var a = 0xFF00FF00FF00FF00FFn;
  var b = 0x00FF00FF00FF00FF00n;
  
  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: 1157442765409226766335
XOR: 1157442765409226766335
NOT a: -1157442765409226766336
Shift left: 2612087089638103044080
Shift right: 1157442765409226766335

Modular Arithmetic

BigInt provides methods for modular arithmetic, including pow and modPow which are essential for cryptographic algorithms.

main.dart
void main() {
  var base = 5n;
  var exponent = 100n;
  var modulus = 101n;
  
  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

Source

Dart BigInt Documentation

This tutorial covered Dart's BigInt with practical examples demonstrating its key features and usage patterns for arbitrary-precision arithmetic.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.