int (int value, int bits, int prevMask) { assert value < (1 << bits) && value >= 0 : value; int mask = (prevMask << bits) | value; if (mask < 0) throw new IllegalStateException("Too many flags, int mask overflown"); return mask; }