countSetBits method

int countSetBits(
  1. int bitWidth
)

Returns the number of set bits in this, assuming a bitWidththis.

NOTE: bitWidth is not validated. See Integral.bitsSet.

Implementation

int countSetBits(int bitWidth) {
  var count = 0;
  for (var i = 0; i < bitWidth; i++) {
    if (this & (1 << i) != 0) {
      count++;
    }
  }
  return count;
}