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 已经很长,则代码是正确的并且 FindBugs 对错误有误)。

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

相关文章:

java - 在没有站点 :site? 的情况下从 Maven 生成 Findbug HTML 报告的任何简单方法

Java 读取 200 万行文本文件的最快方法

java - 使用 Intent.ACTION_OPEN_DOCUMENT 从特定文件夹打开文件

java - 生成 RSA key 对并将 public 编码为字符串

java - Eclipse+FindBugs - 排除过滤器文件不起作用

c - 最低有效位 (LSB) 是否始终为 "first"位?

java - 全长截取 RecyclerView 的屏幕截图

java - 无法清除已检查异常上的 java.sql.Statement

c++ - 位域成员的大小?

x86 - Bit Hack 获取位变化的位置