gpt4 book ai didi

java - Java 中的位旋转

转载 作者:行者123 更新时间:2023-11-29 03:41:51 25 4
gpt4 key购买 nike

来自[隐藏链接——已转变成恶意网站]

When the value to be shifted (left-operand) is an int, only the last 5 digits of the right-hand operand are used to perform the shift. The actual size of the shift is the value of the right-hand operand masked by 31 (0x1f). Ie the shift distance is always between 0 and 31 (if shift value is > 32 shift is 32 % value)

35             00000000 00000000 00000000 00100011
31 -> 0x1f 00000000 00000000 00000000 00011111
& -----------------------------------
Shift value 00000000 00000000 00000000 00000011 -> 3

-29 11111111 11111111 11111111 11100011
31 -> 0x1f 00000000 00000000 00000000 00011111
& -----------------------------------
Shift value 00000000 00000000 00000000 00000011 -> 3

这到底是什么意思?这是否意味着对于右移,您最多可以乘以 32?这是为了防止溢出吗?

最佳答案

不是你可以乘以的最大值是 32,而是 2^31。 (位移位是求幂,不是乘法)

考虑到整数只有 32 位,如果移动超过 31 位,您会发生什么情况?无论您的输入值如何,您最终都会得到完全相同的结果 - 它的所有位都将被“移到末尾” - 因此它作为一个操作将毫无用处。

关于java - Java 中的位旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797835/

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