bitLength property Null safety

int bitLength
override

Returns the minimum number of bits required to store this 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 the number of bits needed to store the value as a signed value, add one, i.e. use x.bitLength + 1.

Implementation

@override
int get bitLength {
  if (isZero) return 0;
  int a0 = _l, a1 = _m, a2 = _h;
  if (isNegative) {
    a0 = _MASK & ~a0;
    a1 = _MASK & ~a1;
    a2 = _MASK2 & ~a2;
  }
  if (a2 != 0) return _BITS01 + a2.bitLength;
  if (a1 != 0) return _BITS + a1.bitLength;
  return a0.bitLength;
}