bitLength property

int? get bitLength

Returns the minimum number of bits required to store this big integer.

The number of bits excludes the sign bit, which gives the natural length for non-negative (unsigned) values. Negative values are complemented to return the bit position of the first bit that differs from the sign bit.

To find the number of bits needed to store the value as a signed value, add one, i.e. use x.bitLength + 1.

x.bitLength == (-x-1).bitLength;

Obj(BigInt.from(3)).bitLength == 2;   // 00000011
Obj(BigInt.from(2)).bitLength == 2;   // 00000010
Obj(BigInt.from(1)).bitLength == 1;   // 00000001
Obj(BigInt.from(0)).bitLength == 0;   // 00000000
Obj(BigInt.from(-1)).bitLength == 0;  // 11111111
Obj(BigInt.from(-2)).bitLength == 1;  // 11111110
Obj(BigInt.from(-3)).bitLength == 2;  // 11111101
Obj(BigInt.from(-4)).bitLength == 2;  // 11111100

Implementation

int? get bitLength => value?.bitLength;