tags:

views:

53

answers:

2

Can the following

<?=($Num&1) ? "odd" : "even"?>

translate to an equivalent in Java?

+5  A: 

String s = (num % 2 == 0) ? "even" : "odd";

To be thorough, that isn't the same operator that you're using (bitwise AND). Bitwise AND is the same in java, so you could also write your php line like:

String s = (num & 1 == 0) ? "even" : "odd";

Sam C
+2  A: 

A direct translation would be:

string s = ( num & 1 != 0 ) ? "odd" : "even"

* note: not entirely sure if the !=0 part is strictly necessary.

slebetman