gpt4 book ai didi

Java 当向左移动位时,溢出不会消失但会出现在另一边

转载 作者:行者123 更新时间:2023-11-29 08:33:12 25 4
gpt4 key购买 nike

int i = 0x000000FF;
i = i << 24;

message = String.format("0x%08X", i);
// prints the message 0xFF000000

int i = 0x000000FF;
i = i << 32;

message = String.format("0x%08X", i);
// prints the message 0x000000FF

我期待第二个打印 0x00000000

这是怎么回事?

最佳答案

参见 Java 语言规范 15.19. Shift Operators :

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.

所以,i << 32i << 0相同,即根本没有变化。

关于Java 当向左移动位时,溢出不会消失但会出现在另一边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46250443/

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