Given the size of a mask of size size I'd like to create a long mask with the bottom size bits of the mask set, and the upper bits zero. For example example, the mask for size == 4 is 0b1111 (aka 0x0000000000000000F), and so on. size must be in the range 0 <= size <= 64.
A typical attempt that isn't correct for all inputs is as follows:
long makeMask(int size) {
return (1L << size) - 1;
}
... but this fails for size == 64, returning 0 rather than the expected all-ones: 0xFFFFFF..... For int masks, I could cast to long to avoid this issue, but for long I don't see a similar workaround.
I could do something like:
long makeMask(int size) {
return size == 64 ? -1L : (1L << size) - 1;
}
... but I'd really like to avoid an unpredictable branch in there.