Java simplify the syntax
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 boolean
s, 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) )
...
精彩评论