gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:04:35 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 已经是 long,则代码是正确的,FindBugs 对该 bug 的判断是错误的)。

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

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