public void encodeBounded(int max, final int value) throws IOException {
        int im = 1; // integer mask
        while (im <= max) {
            if (bm == 0x100) {
                writeByte((byte) b);
                bm = 1;
                b = 0;
            }
            if ((value & im) != 0) {
                b |= bm;
                max -= im;
            }
            bm <<= 1;
            im <<= 1;
        }
    }
DataOutputStream for fast-compact encoding of number sequences

@author ab
main/src/main/java/cgeo/geocaching/brouter/util/MixCoderDataOutputStream.java