appendBits method

void appendBits(
  1. int value,
  2. int numBits
)

Appends the least-significant bits, from value, in order from most-significant to least-significant. For example, appending 6 bits from 0x000001E will append the bits 0, 1, 1, 1, 1, 0 in that order.

@param value int containing bits to append @param numBits bits from value to append

Implementation

void appendBits(int value, int numBits) {
  if (numBits < 0 || numBits > 32) {
    throw ArgumentError(r'Num bits must be between 0 and 32');
  }

  int nextSize = _size;
  _ensureCapacity(nextSize + numBits);
  for (int numBitsLeft = numBits - 1; numBitsLeft >= 0; numBitsLeft--) {
    if ((value & (1 << numBitsLeft)) != 0) {
      bits[nextSize ~/ 32] |= 1 << (nextSize & 0x1F);
    }
    nextSize++;
  }
  _size = nextSize;
}