public final void encodeBounded(int max, final int value) {
        int im = 1; // integer mask
        while (im <= max) {
            if ((value & im) != 0) {
                encodeBit(true);
                max -= im;
            } else {
                encodeBit(false);
            }
            im <<= 1;
        }
    }
encode an integer in the range 0..max (inclusive).
For max = 2^n-1, this just encodes n bits, but in general
this is variable length encoding, with the shorter codes
for the central value range
main/src/main/java/cgeo/geocaching/brouter/util/BitCoderContext.java