Dart BigInt bitLength
Syntax & Examples

BigInt.bitLength property

The `bitLength` property returns the minimum number of bits required to store a big integer.


Syntax of BigInt.bitLength

The syntax of BigInt.bitLength property is:

 int bitLength 

This bitLength property of BigInt returns the minimum number of bits required to store this big integer.

Return Type

BigInt.bitLength returns value of type int.



✐ Examples

1 Calculating Bits for Positive and Negative Numbers

In this example,

  1. We create two BigInt objects, num1 and num2, initialized with 100 and -100 respectively.
  2. We then use the bitLength property to calculate the number of bits required to store these numbers.
  3. We print the results to standard output.

Dart Program

void main() {
  BigInt num1 = BigInt.from(100);
  BigInt num2 = BigInt.from(-100);
  int bits1 = num1.bitLength;
  int bits2 = num2.bitLength;
  print('Bits required to store 100: $bits1');
  print('Bits required to store -100: $bits2');
}

Output

Bits required to store 100: 7
Bits required to store -100: 7

2 Calculating Bits for a Positive Number

In this example,

  1. We create a BigInt object, num, initialized with 255.
  2. We then use the bitLength property to calculate the number of bits required to store this number.
  3. We print the result to standard output.

Dart Program

void main() {
  BigInt num = BigInt.from(255);
  int bits = num.bitLength;
  print('Bits required to store 255: $bits');
}

Output

Bits required to store 255: 8

3 Calculating Bits for a Large Number

In this example,

  1. We create a BigInt object, num, by parsing the string '123456789012345678901234567890'.
  2. We then use the bitLength property to calculate the number of bits required to store this large number.
  3. We print the result to standard output.

Dart Program

void main() {
  BigInt num = BigInt.parse('123456789012345678901234567890');
  int bits = num.bitLength;
  print('Bits required to store a large number: $bits');
}

Output

Bits required to store a large number: 97

Summary

In this Dart tutorial, we learned about bitLength property of BigInt: the syntax and few working examples with output and detailed explanation for each example.