gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:37:35 25 4
gpt4 key购买 nike

运行 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 位将转换为 位的移位,即没有变化。

关于java - 有人可以解释这种 shiftwise/Long 修补输出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33823593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com