Java simplifies syntax

What would be the hardest way in Java to check the following condition

int m, n; 

The test condition is whether m or n are negative, but both of them should not be negative. I am looking for simple but simple syntax

+4
source share
4 answers
 (m < 0) ^ (n < 0) 

Note that in this context ^ is a logical XOR operator (yes, I mean "logical" other than "bitwise").

+15
source
 (m ^ n) < 0 

Even more placeholder to respond to the appropriate length.

+12
source

I would go for:

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

!= works the same as ^ for boolean s, but I think it is easier to understand and use more often.

+2
source

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

Here is a test expressed in java;

 if ( (x & Integer.MIN_VALUE) != (y & Integer.MIN_VALUE) ) ... 
+1
source

All Articles