有人可以解释这个 shiftwise/Long 修补输出吗?

Can someone explain this shiftwise/Long tinkering output?

运行 1:

public static void main(String[] args) {

        System.out.println("shiftwise Example A = " + (0x47494638 << 32));

        long someNumber = 0x47494638;
        long otherNumber = someNumber << 32;

        System.out.println("shiftwise Example B = " + otherNumber);



    }

输出:
shiftwise 示例 A = 1195984440
shiftwise 示例 B = 5136714056324874240

运行 2:(我刚刚在示例 A 中指定了 'L'):

public static void main(String[] args) {

        System.out.println("shiftwise Example A = " + (0x47494638L << 32));

        long someNumber = 0x47494638;
        long otherNumber = someNumber << 32;

        System.out.println("shiftwise Example B = " + otherNumber);



    }

输出:
shiftwise 示例 A = 5136714056324874240
shiftwise 示例 B = 5136714056324874240

如果您不指定 L 后缀,则常量文字值将被视为 int,并且来自 §15.19 of the Java Language Specification 的文本适用:

If the promoted type of the left-hand operand is int, then only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.

您左移 32 位因此转换为 位的移位,即没有变化。

该值被认为是一个int,可以这样模拟:

运行 3:

public static void main(String[] args) {
    System.out.println("shiftwise Example A = " + (0x47494638 << 32));

    int someNumber = 0x47494638;
    long otherNumber = someNumber << 32;

    System.out.println("shiftwise Example B = " + otherNumber);
}

输出:

shiftwise Example A = 1195984440
shiftwise Example B = 1195984440