@Test public void getNumSetBits_TestAllBytes() { for (int number = 0; number <= 255; number++) { int expected = 0; for (char c : Integer.toBinaryString(number).toCharArray()) { if (c == '1') { expected++; } } assertEquals(expected, NumSetBits.getNumSetBits((byte) number, 0, 7)); } }
public static long getNumSetBits(byte[] leds, long offset, long length) { 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; }
NumSetBits { public static long getNumSetBits(byte[] leds, long offset, long length) { 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; } }
NumSetBits { public static long getNumSetBits(byte[] leds, long offset, long length) { 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; }  }
NumSetBits { public static long getNumSetBits(byte[] leds, long offset, long length) { 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; }  static long getNumSetBits(byte[] leds, long offset, long length); static int getNumSetBits(byte b, int startOffset, int endOffset); static long getNumSetBitsBetweenWholeBytes(byte[] array,
			int firstByte, int lastByte); }
NumSetBits { public static long getNumSetBits(byte[] leds, long offset, long length) { 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; }  static long getNumSetBits(byte[] leds, long offset, long length); static int getNumSetBits(byte b, int startOffset, int endOffset); static long getNumSetBitsBetweenWholeBytes(byte[] array,
			int firstByte, int lastByte);  }