private static byte[] unpackHLLRegister(InputStream in, int length, int bitSize)
      throws IOException {
    int mask = (1 << bitSize) - 1;
    int bitsLeft = 8;

    if (bitSize == 8) {
      return fastPathRead(in, length);
    }

    byte current = (byte) (0xff & in.read());

    byte[] output = new byte[length];
    for (int i = 0; i < output.length; i++) {
      byte result = 0;
      int bitsLeftToRead = bitSize;
      while (bitsLeftToRead > bitsLeft) {
        result <<= bitsLeft;
        result |= current & ((1 << bitsLeft) - 1);
        bitsLeftToRead -= bitsLeft;
        current = (byte) (0xff & in.read());
        bitsLeft = 8;
      }
      if (bitsLeftToRead > 0) {
        result <<= bitsLeftToRead;
        bitsLeft -= bitsLeftToRead;
        result |= (current >>> bitsLeft) & ((1 << bitsLeftToRead) - 1);
      }
      output[i] = (byte) (result & mask);
    }
    return output;
  }
Unpack the bitpacked HyperLogLog register.
@param in
         - input stream
@param length
         - serialized length
@return unpacked HLL register
@throws IOException
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/common/ndv/hll/HyperLogLogUtils.java