setRange method

void setRange(
  1. int start,
  2. int end
)

Sets a range of bits.

@param start start of range, inclusive. @param end end of range, exclusive

Implementation

void setRange(int start, int end) {
  if (end < start || start < 0 || end > _size) {
    throw ArgumentError(r'Illegal Argument');
  }
  if (end == start) {
    return;
  }
  end--; // will be easier to treat this as the last actually set bit -- inclusive
  final firstInt = start ~/ 32;
  final lastInt = end ~/ 32;
  for (int i = firstInt; i <= lastInt; i++) {
    final firstBit = i > firstInt ? 0 : start & 0x1F;
    final lastBit = i < lastInt ? 31 : end & 0x1F;
    // Ones from firstBit to lastBit, inclusive
    final mask = (2 << lastBit) - (1 << firstBit);
    _bits[i] |= mask;
  }
}