public static long pop_array(long A[], int wordOffset, int numWords) {
    /*
    * Robert Harley and David Seal's bit counting algorithm, as documented
    * in the revisions of Hacker's Delight
    * http://www.hackersdelight.org/revisions.pdf
    * http://www.hackersdelight.org/HDcode/newCode/pop_arrayHS.cc
    *
    * This function was adapted to Java, and extended to use 64 bit words.
    * if only we had access to wider registers like SSE from java...
    *
    * This function can be transformed to compute the popcount of other functions
    * on bitsets via something like this:
    * sed 's/A\[\([^]]*\)\]/\(A[\1] \& B[\1]\)/g'
    *
    */
    int n = wordOffset + numWords;
    long tot = 0, tot8 = 0;
    long ones = 0, twos = 0, fours = 0;

    int i;
    for (i = wordOffset; i <= n - 8; i += 8) {
      /***  C macro from Hacker's Delight
       #define CSA(h,l, a,b,c) \
       {unsigned u = a ^ b; unsigned v = c; \
       h = (a & b) | (u & v); l = u ^ v;}
       ***/

      long twosA, twosB, foursA, foursB, eights;

      // CSA(twosA, ones, ones, A[i], A[i+1])
      {
        long b = A[i], c = A[i + 1];
        long u = ones ^ b;
        twosA = (ones & b) | (u & c);
        ones = u ^ c;
      }
      // CSA(twosB, ones, ones, A[i+2], A[i+3])
      {
        long b = A[i + 2], c = A[i + 3];
        long u = ones ^ b;
        twosB = (ones & b) | (u & c);
        ones = u ^ c;
      }
      //CSA(foursA, twos, twos, twosA, twosB)
      {
        long u = twos ^ twosA;
        foursA = (twos & twosA) | (u & twosB);
        twos = u ^ twosB;
      }
      //CSA(twosA, ones, ones, A[i+4], A[i+5])
      {
        long b = A[i + 4], c = A[i + 5];
        long u = ones ^ b;
        twosA = (ones & b) | (u & c);
        ones = u ^ c;
      }
      // CSA(twosB, ones, ones, A[i+6], A[i+7])
      {
        long b = A[i + 6], c = A[i + 7];
        long u = ones ^ b;
        twosB = (ones & b) | (u & c);
        ones = u ^ c;
      }
      //CSA(foursB, twos, twos, twosA, twosB)
      {
        long u = twos ^ twosA;
        foursB = (twos & twosA) | (u & twosB);
        twos = u ^ twosB;
      }

      //CSA(eights, fours, fours, foursA, foursB)
      {
        long u = fours ^ foursA;
        eights = (fours & foursA) | (u & foursB);
        fours = u ^ foursB;
      }
      tot8 += pop(eights);
    }

    // handle trailing words in a binary-search manner...
    // derived from the loop above by setting specific elements to 0.
    // the original method in Hackers Delight used a simple for loop:
    //   for (i = i; i < n; i++)      // Add in the last elements
    //  tot = tot + pop(A[i]);

    if (i <= n - 4) {
      long twosA, twosB, foursA, eights;
      {
        long b = A[i], c = A[i + 1];
        long u = ones ^ b;
        twosA = (ones & b) | (u & c);
        ones = u ^ c;
      }
      {
        long b = A[i + 2], c = A[i + 3];
        long u = ones ^ b;
        twosB = (ones & b) | (u & c);
        ones = u ^ c;
      }
      {
        long u = twos ^ twosA;
        foursA = (twos & twosA) | (u & twosB);
        twos = u ^ twosB;
      }
      eights = fours & foursA;
      fours = fours ^ foursA;

      tot8 += pop(eights);
      i += 4;
    }

    if (i <= n - 2) {
      long b = A[i], c = A[i + 1];
      long u = ones ^ b;
      long twosA = (ones & b) | (u & c);
      ones = u ^ c;

      long foursA = twos & twosA;
      twos = twos ^ twosA;

      long eights = fours & foursA;
      fours = fours ^ foursA;

      tot8 += pop(eights);
      i += 2;
    }

    if (i < n) {
      tot += pop(A[i]);
    }

    tot += (pop(fours) << 2) + (pop(twos) << 1) + pop(ones) + (tot8 << 3);

    return tot;
  }
Returns the number of set bits in an array of longs.
ambry-utils/src/main/java/com/github/ambry/utils/BitUtil.java