public int decodeBounded(final int max) throws IOException {
        int value = 0;
        int im = 1; // integer mask
        while ((value | im) <= max) {
            if (decodeBit()) {
                value |= im;
            }
            im <<= 1;
        }
        return value;
    }
decode an integer in the range 0..max (inclusive).
main/src/main/java/cgeo/geocaching/brouter/util/MixCoderDataInputStream.java