The `bitLength` property returns the minimum number of bits required to store a big integer.
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.
BigInt.bitLength returns value of type int
.
In this example,
num1
and num2
, initialized with 100 and -100 respectively.bitLength
property to calculate the number of bits required to store these numbers.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');
}
Bits required to store 100: 7 Bits required to store -100: 7
In this example,
num
, initialized with 255.bitLength
property to calculate the number of bits required to store this number.void main() {
BigInt num = BigInt.from(255);
int bits = num.bitLength;
print('Bits required to store 255: $bits');
}
Bits required to store 255: 8
In this example,
num
, by parsing the string '123456789012345678901234567890'.bitLength
property to calculate the number of bits required to store this large number.void main() {
BigInt num = BigInt.parse('123456789012345678901234567890');
int bits = num.bitLength;
print('Bits required to store a large number: $bits');
}
Bits required to store a large number: 97
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.