learningtocode
learningtocode

Reputation: 765

What type is bit shift operator output?

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

Answers (1)

M A
M A

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

Related Questions