Bitwise operators are offered by Java. We can easily manipulate a number’s individual bits thanks to those operators.

However, we might make a common mistake when comparing the outcome of a bitwise operation.

In this brief tutorial, we will cover the causes of and solutions for the Java compile-time error “bad operand types for binary operator”.

An overview of the issue

An illustration will help us to comprehend the issue. But first, let’s look at a straightforward approach:

public void checkNumber() {
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
intList.forEach(i -> {
if (i & 1 == 1) {
System.out.println(i + " is odd.");
} else {
System.out.println(i + " is even.");
}
});
}

As we can see, the checkNumber method iterates through the intList, determining whether each number is even or odd and printing the results.

We should be aware that the method does not use a typical implementation of the odd-checking logic: i % 2 == 1. Instead, we perform the bitwise AND (&) operation on an Integer number (i) and 1. We know the integer i is an odd number if the outcome is 1: i & 1 ==1.

But when we attempt to test the aforementioned procedure, the code shockingly fails to compile:

java: bad operand types for binary operator '&' 
first type: java.lang.Integer 
second type: boolean

Next, let’s examine the issue’s root cause and proposed solutions.

Recognizing Java Operator Priority

The error message is rather simple to begin with. It states that we try to do a bitwise AND on both boolean and Integer types.

But it’s strange because we really put “i & 1” in the code. Why does the compiler believe that the bitwise AND operation involves a boolean type?

The reason for this is that the“==” operator has priority over the“&” operator. That is to say the expression “i & 1 == 1” is the same as “i & (1 == 1)“. Thus, we have “i & true (boolean)“.

“Ok, == has greater precedence than &,” we can now inquire. But why does ‘i % 2 == 1‘ perform as anticipated?

We must examine the precedence rule for Java operators in further detail in order to respond to that query.

There are a good number of operators available in Java. In reality, we frequently combine various operators. Understanding the Java operators’ order of precedence is crucial. Otherwise, we risk getting an unexpected outcome.

Let’s now examine the Java operator precedence rule, which states that an operator has greater precedence the higher in the table it occurs in:

bad operand types for binary operatorThe modulo operator (%) has higher precedence than the equality operator (==), as we can see in the list above. The bitwise AND operator (&), on the other hand, is included in the table below the equality operator (==).

That’s why “i % 2 == 1” works as expected but “i & 1 == 1” does not.

In our case, we ran across a compile-time problem. As a result, we can identify the issue rather quickly. Imagine, however, that an implementation with an operator precedence problem compiles but yields an incorrect outcome. It can take us a long time to identify the root of the issue.

Therefore, it’s important to remember the Java operator precedence rule.

Resolving the Issue

Now that we know what caused the issue, solving it won’t be difficult. The bitwise AND operator just requires the addition of parentheses:

if (i & 1 == 1) --> if ((i & 1) == 1)

If we run the procedure again after making the necessary corrections, we’ll find that the compiler no longer raises an error and that the desired result is produced:

1 is odd.
2 is even.
3 is odd.
4 is even.
5 is odd.
6 is even.
7 is odd.

Conclusion

In this quick article, we’ve analyzed the compilation error “bad operand types for binary operator” through a bitwise AND operation example. Further, we’ve discussed Java operators’ precedence rule. Hope you will have great experience about it. If you are having trouble with this experience, you can contact Onextdigital, which is always ready to help you overcome any barriers.

Additionally, Onextdigital is the only company you need to contact if you want to expand your economy or establish a website for your eCommerce business. Along with a team of qualified specialists and developers who work closely with the designers, we also provide affordable Shopify Development, Bigcommerce Development, and Magento Development services.

> Read also

Java Random Number: Methods To Generate It