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

标签 java findbugs bit

我正在使用 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 已经是 long,则代码是正确的,FindBugs 对该 bug 的判断是错误的)。

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

相关文章:

java - 如何将上传的图像保存到我的 C 盘

java - 如何修复直接写入 HTTP header 输出的 Findbugs HTTP 参数

java - 写入静态字段 - 在这种情况下 FindBugs 是错误的吗?

java - FindBugs 和静态初始化顺序

bit - 经典计算机在单个时间点可以考虑多少个状态?

algorithm - 检查 2 的幂数

java - 如何读取服务器java.nio中的字符集UTF-8编码缓冲区

javascript - 庆典 : jest: command not found

algorithm - 绘制给定区域的像素圆

java - 如何从 OSM Tiles 在 Java 应用程序中创建 map ?