Java 32 forward offset

I am trying to change the offset of an integer to 32, but the result is equal to the same number. (for example, 5 >> 32equal to 5.)

If I try to perform the same operation on byte and short, it will work. For example, "(byte) 5 → 8" is 0.

What is wrong with Integer?

+4
source share
2 answers

JLS 15.19. Shift operators

... If the advanced type of the left operand is int, only the five least significant bits of the right operand are used as the offset distance .

therefore, the shift is 32ineffective.

+9
source

A int long. , byte, int .

Java:

public static void main(String s[]) {
    byte b = 5;
    System.out.println(b >> 8);
    int i = 8;
    System.out.println(i >> 32);
}

-:

         0: iconst_5
         1: istore_1
         2: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream; 
         5: iload_1
         6: bipush        8
         8: ishr
         9: invokevirtual #22      // Method java/io/PrintStream.println:(I)V  ==> Using println(int)
        12: bipush        8
        14: istore_2
        15: getstatic     #16     // Field java/lang/System.out:Ljava/io/PrintStream;
        18: iload_2
        19: bipush        32
        21: ishr
        22: invokevirtual #22      // Method java/io/PrintStream.println:(I)V   ==> Using println(int)
        25: return
0

Source: https://habr.com/ru/post/1607985/


All Articles