Reputation: 765
I have the following code
public static void main(String[] args){
//find sign of 32 bit integer
int a=0x6348987C;
a=a>>31;
int one=a&1;
if( one==0 )
{
System.out.println("Positive");
}
else
{
System.out.println("Negative");
}
}
If I change the comparison line with if( a&1 ==0 )
the compilation fails with operator & cannot be applied to int,boolean, mking me wonder what is the resulting type of the shift operator.
Upvotes: 1
Views: 113
Reputation: 72844
The ==
equality operator precedes the bitwise AND operator in terms of operator precedence (see this page). You should place parentheses to make the latter precede:
if ((a & 1) == 0) {
Note that the type of the result of a & 1
is the same type of one
in the original code (int
).
Upvotes: 2