public final int decodeBounded(final int max) {
        int value = 0;
        int im = 1; // integer mask
        while ((value | im) <= max) {
            if (bits == 0) {
                bits = 8;
                b = ab[++idx] & 0xff;
            }
            if ((b & 1) != 0) {
                value |= im;
            }
            b >>>= 1;
            bits--;
            im <<= 1;
        }
        return value;
    }
decode an integer in the range 0..max (inclusive).

@see #encodeBounded
main/src/main/java/cgeo/geocaching/brouter/util/BitCoderContext.java