toIntUnsigned method

int toIntUnsigned(
  1. int width
)

Returns this BigInt as an int.

Always interprets the number as unsigned, and thus never clamps to fit.

Implementation

int toIntUnsigned(int width) {
  if (width > INT_BITS) {
    throw Exception('Cannot convert Int when width $width'
        ' is greater than $INT_BITS');
  } else if (width == INT_BITS && INT_BITS > 32) {
    assert(!kIsWeb, 'This code is not set up to work with JS.');

    // When width is 64, `BigInt.toInt()` will clamp values assuming that
    // it's a signed number.  To avoid that, if the width is 64, then do the
    // conversion in two 32-bit chunks and bitwise-or them together.
    const maskWidth = 32;
    final mask = _BigLogicValue._maskOfWidth(maskWidth);
    return (this & mask).toInt() |
        (((this >> maskWidth) & mask).toInt() << maskWidth);
  } else {
    return (this & _BigLogicValue._maskOfWidth(width))
        .toInt()
        .toSigned(INT_BITS);
  }
}