It's Java exploiting compiler optimisations from the C and C++ worlds. For a 32 bit int
, using an bit-shift argument greater than or equal to 31 will set the resulting value to 0 for a positive int
. (For a negative argument the behaviour in C and C++ on shifting is implementation defined).
Whereas in C and C++, actually using a value greater than 31 for a 32 bit int
is in fact undefined behaviour, the Java bods have actually defined the behaviour specifically and simply perform the shift with an argument modulo 32 (which is what the majority of C and C++ compilers actually do). This method is mentioned explicitly in the JLS snippet you've quoted.
Extracting the lowest five order bits of a number is equivalent to taking that number modulo 32.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…