Dart int bitLength
Syntax & Examples


int.bitLength property

The `bitLength` property in Dart returns the minimum number of bits required to store this integer.


Syntax of int.bitLength

The syntax of int.bitLength property is:

 int bitLength 

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

Return Type

int.bitLength returns value of type int.



✐ Examples

1 Bit length of a positive integer

In this example,

  1. We create an integer variable number with the value 15.
  2. We access its bitLength property to get the minimum number of bits required to store it.
  3. We then print the result to standard output.

Dart Program

void main() {
  int number = 15;
  int bits = number.bitLength;
  print('Bit length of $number: $bits');
}

Output

Bit length of 15: 4

2 Bit length of a large positive integer

In this example,

  1. We create an integer variable largeNumber with the value 1000000.
  2. We access its bitLength property to get the minimum number of bits required to store it.
  3. We then print the result to standard output.

Dart Program

void main() {
  int largeNumber = 1000000;
  int bitsLarge = largeNumber.bitLength;
  print('Bit length of $largeNumber: $bitsLarge');
}

Output

Bit length of 1000000: 20

3 Bit length of a negative integer

In this example,

  1. We create an integer variable negativeNumber with the value -8.
  2. We access its bitLength property to get the minimum number of bits required to store it.
  3. We then print the result to standard output.

Dart Program

void main() {
  int negativeNumber = -8;
  int bitsNegative = negativeNumber.bitLength;
  print('Bit length of $negativeNumber: $bitsNegative');
}

Output

Bit length of -8: 3

Summary

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