Is it possible to create a bitmask for ~ 100 constants?
You cannot do this directly, because the maximum size for a primitive number that can be used as a bit mask is actually 64 bits for the value long
. What you can do is split the bitmask into 2 or more int
or long
and then manipulate it manually.
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;
}
source to share
You can create a simple bit mask with the number of bits in the primitive type.
If you have a 32-bit (as in normal Java) int, then 1 <31, you can shift the bottom bit.
To have large constants, you use an array of int elements, and you determine which element of the array to use by dividing by 32 (with a 32-bit int) and swapping from% 32 (modula) to the selected element of the array.
source to share