Java – can I create a bitmask for ~ 100 constants?

Does this mean that the 100th constant must be 1 < < 100?

Solution

You cannot do this directly because the maximum size of the original number that can be used as a bit mask is actually a long 64 bit value What you can do is divide the bitmask into 2 or more integers or long integers and manage them 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;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>