gpt4 book ai didi

java - FindBugs:INT_VACUOUS_BIT_OPERATION

转载 作者:行者123 更新时间:2023-11-29 03:36:16 26 4
gpt4 key购买 nike

为了将 int 转换为 IP 字符串,我使用了 Going from 127.0.0.1 to 2130706433, and back again 中的方法

private static final byte BYTE_MASK = (byte)0xff;

protected byte[] unpack(int bytes) {
return new byte[] {
(byte)((bytes >>> 24) & BYTE_MASK),
(byte)((bytes >>> 16) & BYTE_MASK),
(byte)((bytes >>> 8) & BYTE_MASK),
(byte)((bytes ) & BYTE_MASK)
};
}

但是 Eclipse 中的 FindBugs 会生成错误:INT_VACUOUS_BIT_OPERATION

INT_VACUOUS_BIT_OPERATION: bit operations that don't do any meaningful work.

为什么会这样以及如何解决?

最佳答案

我怀疑这是因为如果您转换为byte,则不需要& BYTE_MASK。我假设 BYTE_MASK 是 0xff... 在这种情况下它基本上毫无意义。仅施法将具有相同的效果。

来自 section 5.1.3 of the JLS :

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

关于java - FindBugs:INT_VACUOUS_BIT_OPERATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15360073/

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