In RegularEnumSet implementation, there is a code:
elements = -1L >>> -universe.lengh
It uses a Long type integer to implement efficient EnumSet.
What is notable is that the right-hand operand of >>> is a negative figure.
I have tested and found that
int i = -1;
i >>> -3
has equivalent effect with
int i = 1;
i << 3;
So why bother to write in this ambiguous form?
From the “Java Language Specification”, section 15.19, about shifting “If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-and operand were subjected to a bitwise logical AND operator & with the mask value 0x3f. The shift distance actually used is therefore awlays 0 to 64, inclusive.”
So,
>>> -3has equivalent effect as>>> 61, and-1L >>> -nequals-1L >>> (0x3f&-n)Actually this seems to be the most effective way of producing a consecutive
1sin the lowest n-bits of a long integer.