Findbugs警告:整数移位32 - 这是什么意思? [英] Findbugs warning: Integer shift by 32 -- what does it mean?

查看:380
本文介绍了Findbugs警告:整数移位32 - 这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Findbugs扫描第三方源代码(只是为了在集成之前要谨慎),并发现以下警告:

I was scanning a third party source code using Findbugs (just to be cautious before integrating into it mine), and found the following warning:

long a = b << 32 | c




错误:整数移位32模式ID:
ICAST_BAD_SHIFT_AMOUNT,类型:BSHIFT,
类别:正确

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

代码在
0范围之外的
a常量金额执行整数移位。 0.31。这样做的结果是使用整数值
的低5位来决定转移多少。这个
可能不是预期的,而b $ b至少令人困惑。

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.

有谁可以解释上面究竟是什么意思?

Could anyone please explain what exactly does the above mean?

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

Thanks! (I am quite a newbie in Java programming)

推荐答案

来自 Java语言规范


如果左侧操作数的提升类型为int,则只使用右侧操作数的五个最低位作为移位距离。就好像右手操作数受到按位逻辑AND运算符& (§15.22.1),掩码值为0x1f。因此实际使用的移位距离始终在0到31的范围内。

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,则表达式为相同于

So if b is an int, the expression is identical to

long a = b | c;

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

which I highly doubt is what is intended. It should probably have been

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

(如果b已经很长,代码是正确的,并且FindBugs错误地认为该错误)。

(If b is already a long, the code is correct and FindBugs is mistaken about the bug).

这篇关于Findbugs警告:整数移位32 - 这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆