code: private static byte[] bitSetToByteArray(BitSet bitSet) {
  
      while (bitSet.length() % 8 != 0) { bitSet.set(bitSet.length(), true); }
      
      byte[] array = new byte[bitSet.length()/8];
  
      for (int i = 0; i < array.length; i++) {
        int offset = i * 8;
  
        int index = 0;
        for (int j = 0; j < 8; j++) {
          index <<= 1;        
          if (bitSet.get(offset+j)) { index++; }
        }
  
        array[i] = (byte)(index - 128);
      }
  
      return array;
    }
docs: helper for gap encoding.