class NumSetBits {

	public static long getNumSetBits(byte[] leds, long offset, long length) {
		// s/offset/firstBitIndex/ for readability.
		long firstBitIndex = offset;
		long lastBitIndex = firstBitIndex + length - 1;
		byte firstByteIndex = (byte) (firstBitIndex / 8);
		byte lastByteIndex = (byte) ((firstBitIndex + length - 1) / 8);

		long totalSetBits = 0L;
		if (firstByteIndex != lastByteIndex) {
			totalSetBits += getNumSetBitsBetweenWholeBytes(leds,
					firstByteIndex + 1, lastByteIndex - 1);
			totalSetBits += getNumSetBits(leds[firstByteIndex],
					(int) (firstBitIndex % 8), 7);
			totalSetBits += getNumSetBits(leds[lastByteIndex], 0,
					(int) (lastBitIndex % 8));
		} else {
			totalSetBits += getNumSetBits(leds[firstByteIndex],
					(int) (firstBitIndex % 8), (int) (lastBitIndex % 8));
		}

		return totalSetBits;
	}

}