countRange method

int countRange(
  1. int start,
  2. int end, {
  3. bool expected = true,
})

Counts the number of bits set to expected in the range of start (inclusive) to end (exclusive).

Implementation

int countRange(int start, int end, {bool expected = true}) {
  RangeError.checkValidRange(start, end, length);
  if (start == end) return 0;
  final startIndex = start >> bitShift, startBit = start & bitOffset;
  final endIndex = (end - 1) >> bitShift, endBit = (end - 1) & bitOffset;
  var result = 0;
  if (startIndex == endIndex) {
    result += (buffer[startIndex] &
            (((1 << (endBit - startBit + 1)) - 1) << startBit))
        .bitCount;
  } else {
    result += (buffer[startIndex] & (bitMask << startBit)).bitCount;
    for (var i = startIndex + 1; i < endIndex; i++) {
      result += buffer[i].bitCount;
    }
    result += (buffer[endIndex] & ((1 << (endBit + 1)) - 1)).bitCount;
  }
  return expected ? result : (end - start) - result;
}