开发者

Java simplify the syntax

开发者 https://www.devze.com 2023-03-01 04:58 出处:网络
What would be the most terse way in Java to check for the following condition int m, n; The condition to check is whether either m or n are negative but both shouldn\'t be negative. I\'m looking fo

What would be the most terse way in Java to check for the following condition

int m, n;

The condition to check is whether either m or n are negative but both shouldn't be negative. I'm looking for a te开发者_C百科rse yet simple syntax


(m < 0) ^ (n < 0)

Note that in this context, ^ is the logical XOR operator (yes, I do mean "logical", distinct from "bitwise").


(m ^ n) < 0

Even more filler to make an appropriate length answer.


I'd go for:

(m < 0) != (n < 0)

!= operates the same as ^ for booleans, but I think it's easier to understand and more commonly used.


Basically your test should be - the sign bit (highest order) shound be different.

Here is the test expressed in java;

if ( (x & Integer.MIN_VALUE) != (y & Integer.MIN_VALUE) )
    ...
0

精彩评论

暂无评论...
验证码 换一张
取 消