Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bad operand types for binary operator "&" java

The error shows this line

 if ((a[0] & 1 == 0) && (a[1] & 1== 0) && (a[2] & 1== 0)){ 

This is the whole code:

public class Ex4 {    public static void main(String[] args) {   int [] a = new int [3];   if(args.length == 3)   {       try{         for(int i = 0; i < args.length; i++)         {         a[i] = Integer.parseInt(args[i]);         }         }         catch(NumberFormatException e){             System.out.println("Wrong Argument");        }       if ((a[0] & 1 == 0) && (a[1] & 1== 0) && (a[2] & 1== 0)){         System.out.println("yes");       }     else {         System.out.println("no");     }   }   else{       System.out.println("Error");     } } } 

I've fixed the code:

if ((a[0] & 1) == 0 && (a[1] & 1) == 0 && (a[2] & 1) == 0){ 

Was an issue with the brackets, updated for anyone in the future.

like image 463
user3364498 Avatar asked Apr 15 '14 21:04

user3364498


People also ask

Can you use any binary operator in Java generic types?

Java doesn't support using the + operator for anything but primitive numeric types and Strings . Here, you can't use the + operator between any arbitrary objects.

What is binary operator in Java?

A binary operator is an operator that operates on two operands and manipulates them to return a result. Operators are represented by special characters or by keywords and provide an easy way to compare numerical values or character strings. Binary operators are presented in the form: Operand1 Operator Operand2.


1 Answers

== has higher precedence than &. You might want to wrap your operations in () to specify how you want your operands to bind to the operators.

((a[0] & 1) == 0) 

Similarly for all parts of the if condition.

like image 127
Sotirios Delimanolis Avatar answered Sep 25 '22 07:09

Sotirios Delimanolis