gpt4 book ai didi

java - Findbugs 警告 : Integer shift by 32 -- what does it mean?

转载 作者:搜寻专家 更新时间:2023-10-30 21:36:57 25 4
gpt4 key购买 nike

我正在使用 Findbugs 扫描第三方源代码(在集成到我的之前要小心),发现以下警告:

long a = b << 32 | c

Bug: Integer shift by 32 Pattern id: ICAST_BAD_SHIFT_AMOUNT, type: BSHIFT, category: CORRECTNESS

The code performs an integer shift by a constant amount outside the range 0..31. The effect of this is to use the lower 5 bits of the integer value to decide how much to shift by. This probably isn't want was expected, and it at least confusing.

谁能解释一下上面的内容到底是什么意思?

谢谢!(我是 Java 编程的新手)

最佳答案

来自Java Language Specification :

If the promoted type of the left-hand operand is int, 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. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

所以如果 b 是一个 int,则表达式等同于

long a = b | c;

我非常怀疑这是什么意思。应该是

long a = ((long) b << 32) | c;

(如果 b 已经很长,则代码是正确的并且 FindBugs 对错误有误)。

关于java - Findbugs 警告 : Integer shift by 32 -- what does it mean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1023373/

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