位和运算符的结果是否为负(在Java中)

时间:2022-10-23 08:12:26

I have the following piece of code:

我有以下一段代码:

int SOME_MASK = 0x0000ffff;

int value = /* some value */;
int something = SOME_MASK & value;

// WHY IS "something" guaranteed to be non-negative ?
if (something != NEGATIVE_CONSTANT) {
    // do something here....
}

I keep getting the FindBugs analysis warning:

我不断收到FindBugs分析警告:

Correctness - Bad comparison of nonnegative value with negative constant This code compares a value that is guaranteed to be non-negative with a negative constant.

正确性——非负值与负常数的错误比较本代码将一个被保证为非负的值与一个负常数进行比较。

The warning pops up for the line where comparing the bitwise AND result with the negative constant.

当比较位和结果与负常数时,会弹出警告。

I am not sure why a Bitwise AND result is guaranteed to be non-negative? is this always the case?

我不知道为什么一个比特和结果一定是非负的?情况总是这样吗?

5 个解决方案

#1


2  

The result of bitwise-AND can be negative, e.g. (-1) & (-1) is definitely -1.

位-和的结果可以是负数,例如(-1)和(-1)肯定是-1。

However, if either operand is nonnegative, the result must also be nonnegative. This is because in 2's complement representation, a negative number must have its 31st bit set, and a nonnegative number must have its 31st bit cleared. Since & will set a bit only if both operand's bit is set, the result is negative if and only if both inputs are negative.

但是,如果任意一个操作数是非负的,那么结果也必须是非负的。这是因为在2的补码表示法中,一个负数必须有它的31位,一个非负数必须有它的31位被清除。因为&将只在两个操作数的位都被设置时设置一个位,所以当且仅当两个输入都为负时,结果为负。

Since SOME_MASK = 0xffff is positive, the result of SOME_MASK & value will never be negative.

因为SOME_MASK = 0xffff是正数,所以SOME_MASK & value的结果永远不会是负数。

#2


3  

When SOME_MASK starts with a '0' bit, the result of SOME_MASK & value must be positive.

当SOME_MASK以“0”位开始时,SOME_MASK & value的结果必须为正数。

#3


2  

something is guaranteed not to be negative because Java's signed int type uses the most significant bit for sign, and that bit is cleared (0) in SOME_MASK. Since something is the result of something being ANDed with that mask, it can't have that bit set, so it can't be negative.

某些东西保证不会是负的,因为Java的带符号int类型使用最重要的位作为符号,并且该位在SOME_MASK中被清除(0)。因为某些东西是与那个蒙版连接的结果,它不能有那个位集,所以它不能是负的。

#4


2  

Can the result of a bitwise AND operator be negative (in Java)

位和运算符的结果是否为负(在Java中)

In general - Yes.

一般规定——是的。

In the case of your example - No.

在你的例子中-不。

In Java, integers are represented in two's complement signed binary representation. One characteristic of this representation is that the most significant (i.e. left-most) bit is one for a negative number and zero for a positive number.

在Java中,整数用2的补符号二进制表示。这种表示的一个特点是,最重要的(即最左的)位是负数的1,正数的0。

In your example, you are ANDing with 0x0000ffff, and that is setting the top 16 bits to zero. That means that the result of that expression cannot be a negative number.

在您的示例中,您使用的是0x0000ffff,这是将前16位设置为零。这意味着表达式的结果不能是负数。


By contrast, if you ANDed with 0xffff0000, the result could be negative.

相比之下,如果您与0xff0000协商,结果可能是负的。

#5


1  

The bitwise AND of two negative integers is always negative. The bitwise AND of two integers, where one or both are positive, is always positive. To see why, think about the binary representation, and what happens to the highest bit.

位和两个负整数的值总是为负的。两个整数的位和,其中一个或两个都是正的,总是正的。要了解原因,请考虑二进制表示,以及最高位的情况。

#1


2  

The result of bitwise-AND can be negative, e.g. (-1) & (-1) is definitely -1.

位-和的结果可以是负数,例如(-1)和(-1)肯定是-1。

However, if either operand is nonnegative, the result must also be nonnegative. This is because in 2's complement representation, a negative number must have its 31st bit set, and a nonnegative number must have its 31st bit cleared. Since & will set a bit only if both operand's bit is set, the result is negative if and only if both inputs are negative.

但是,如果任意一个操作数是非负的,那么结果也必须是非负的。这是因为在2的补码表示法中,一个负数必须有它的31位,一个非负数必须有它的31位被清除。因为&将只在两个操作数的位都被设置时设置一个位,所以当且仅当两个输入都为负时,结果为负。

Since SOME_MASK = 0xffff is positive, the result of SOME_MASK & value will never be negative.

因为SOME_MASK = 0xffff是正数,所以SOME_MASK & value的结果永远不会是负数。

#2


3  

When SOME_MASK starts with a '0' bit, the result of SOME_MASK & value must be positive.

当SOME_MASK以“0”位开始时,SOME_MASK & value的结果必须为正数。

#3


2  

something is guaranteed not to be negative because Java's signed int type uses the most significant bit for sign, and that bit is cleared (0) in SOME_MASK. Since something is the result of something being ANDed with that mask, it can't have that bit set, so it can't be negative.

某些东西保证不会是负的,因为Java的带符号int类型使用最重要的位作为符号,并且该位在SOME_MASK中被清除(0)。因为某些东西是与那个蒙版连接的结果,它不能有那个位集,所以它不能是负的。

#4


2  

Can the result of a bitwise AND operator be negative (in Java)

位和运算符的结果是否为负(在Java中)

In general - Yes.

一般规定——是的。

In the case of your example - No.

在你的例子中-不。

In Java, integers are represented in two's complement signed binary representation. One characteristic of this representation is that the most significant (i.e. left-most) bit is one for a negative number and zero for a positive number.

在Java中,整数用2的补符号二进制表示。这种表示的一个特点是,最重要的(即最左的)位是负数的1,正数的0。

In your example, you are ANDing with 0x0000ffff, and that is setting the top 16 bits to zero. That means that the result of that expression cannot be a negative number.

在您的示例中,您使用的是0x0000ffff,这是将前16位设置为零。这意味着表达式的结果不能是负数。


By contrast, if you ANDed with 0xffff0000, the result could be negative.

相比之下,如果您与0xff0000协商,结果可能是负的。

#5


1  

The bitwise AND of two negative integers is always negative. The bitwise AND of two integers, where one or both are positive, is always positive. To see why, think about the binary representation, and what happens to the highest bit.

位和两个负整数的值总是为负的。两个整数的位和,其中一个或两个都是正的,总是正的。要了解原因,请考虑二进制表示,以及最高位的情况。