Is it possible to create a bitmask for ~100 constants?

bitmask, java, primitive-types

Solution

You can't do it directly because maximum size for a primitive number which can be used as a bitmask is actually 64 bit for a `long` value. What you can do is to split the bitmask into 2 or more `int`s or `long`s and then manage it by hand.

int[] mask = new int[4];
final int MAX_SHIFT = 32;

void set(int b) {
  mask[b / MAX_SHIFT] |= 1 << (b % MAX_SHIFT);
}

boolean isSet(int b) {
  return (mask[b / MAX_SHIFT] & (1 << (b % MAX_SHIFT))) != 0;
}

Problem

Would that mean that the 100th constant would have to be 1 << 100?

Original source