private static ShiftMask getShift(FileWriter fw, int bitWidth, boolean isLong, boolean msbFirst,
                                      int byteIndex, int valueIndex) throws IOException {
        // relative positions of the start and end of the value to the start and end of the byte
        int valueStartBitIndex = (valueIndex * bitWidth) - (8 * (byteIndex));
        int valueEndBitIndex = ((valueIndex + 1) * bitWidth) - (8 * (byteIndex + 1));

        // boundaries of the current value that we want
        int valueStartBitWanted;
        int valueEndBitWanted;
        // boundaries of the current byte that will receive them
        int byteStartBitWanted;
        int byteEndBitWanted;

        int shift;
        int widthWanted;

        if (msbFirst) {
            valueStartBitWanted = valueStartBitIndex < 0 ? bitWidth - 1 + valueStartBitIndex : bitWidth - 1;
            valueEndBitWanted = valueEndBitIndex > 0 ? valueEndBitIndex : 0;
            byteStartBitWanted = valueStartBitIndex < 0 ? 8 : 7 - valueStartBitIndex;
            byteEndBitWanted = valueEndBitIndex > 0 ? 0 : -valueEndBitIndex;
            shift = valueEndBitWanted - byteEndBitWanted;
            widthWanted = Math.min(7, byteStartBitWanted) - Math.min(7, byteEndBitWanted) + 1;
        } else {
            valueStartBitWanted = bitWidth - 1 - (valueEndBitIndex > 0 ? valueEndBitIndex : 0);
            valueEndBitWanted = bitWidth - 1 - (valueStartBitIndex < 0 ? bitWidth - 1 + valueStartBitIndex : bitWidth - 1);
            byteStartBitWanted = 7 - (valueEndBitIndex > 0 ? 0 : -valueEndBitIndex);
            byteEndBitWanted = 7 - (valueStartBitIndex < 0 ? 8 : 7 - valueStartBitIndex);
            shift = valueStartBitWanted - byteStartBitWanted;
            widthWanted = Math.max(0, byteStartBitWanted) - Math.max(0, byteEndBitWanted) + 1;
        }

        int maskWidth = widthWanted + Math.max(0, shift);

        visualizeAlignment(
                fw, bitWidth, valueEndBitIndex,
                valueStartBitWanted, valueEndBitWanted,
                byteStartBitWanted, byteEndBitWanted,
                shift
        );
        return new ShiftMask(shift, genMask(maskWidth, isLong));
    }
\n");
        if (msbFirst) {
            fw.append(" * Packs from the Most Significant Bit first\n");
        } else {
            fw.append(" * Packs from the Least Significant Bit first\n");
        }
        fw.append(" * \n");
        fw.append(" * @author automatically generated\n");
        fw.append(" * @see ByteBasedBitPackingGenerator\n");
        fw.append(" *\n");
        fw.append("
indexr-codegen/src/main/java/io/indexr/vlt/codec/pack/PackingClassGen.java