Win a copy of Design for the Mind this week in the Design forum!

# Bitwise operator precedence

Marcela Blei
Ranch Hand
Posts: 477
In bitwise operations: which is the order of precedence of the following operators: ^| or |^
I tried some examples putting the same sentence with parentheces and without them and trying all of the combinations but all my displaying results give the same value.
Thanks
Marcela

Anonymous
Ranch Hand
Posts: 18944
The order is & ^ |
int i=10; // 1010
int j=11; // 1011
int k=12; // 1100

System.out.println(i ^ j | k); // prints 13 == > (i^j) | k
System.out.println(i | j ^ k); // prints 15 == > i | (j^k)

Marcela Blei
Ranch Hand
Posts: 477
vasansrini:
You are changing the order of the operators! Since 10^11 isn�t the same as 11^12
But look that:

So that�s a good example: Now we have to compile and run the same sentence without parentheces.
Thank you very much.

Anonymous
Ranch Hand
Posts: 18944
I thought you were asking about the opreator precedence.
If you are going to put paranthesis yourself,then you are taking care of the precedence.

Marcela Blei
Ranch Hand
Posts: 477
Yes, it�s operator precedence what I am talking about. The result of compiling the same sentence without parentheces gives the precedence order, if the result is: 13 then the order is ^|, else is |^. I asked that question because Eckel�s book says one thing and Jaworski says the opposite and I did�t find the rules in the JLS.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
The JLS does a remarkable job of hiding this information, actually. In fact it seems to list the operator in decreasing order of precedence in sections 15.8 through 15.26, without actually saying that this is the order of precedence. The official confirmation of this is buried in the syntax specifications at each level. For example, in section 15.17 we have:
<pre>MultiplicativeExpression:
UnaryExpression
MultiplicativeExpression * UnaryExpression
MultiplicativeExpression / UnaryExpression
MultiplicativeExpression % UnaryExpression
</pre>
This means that a "MultiplicativeExpression" can be a "UnaryExpression", or a MultiplicativeExpression followed by "8", "/', or "%", followed by a UnaryExpression. Which means that the only way the compiler can parse a *, /, or % is if it has already resolved any unary expressions before this. Which means that all unary expressions have higher precedence than the multiplicative operators.
And in section 15.18:
MultiplicativeExpression
</pre>
Here we see that in order to resolve a + or -, the compiler must first resolve any adjacent MultiplicativeExpressions. So *, /, and % have higher precedence than + or -.
So to get all this info from the JLS, you have to go through each of these syntax specifications given in sections. Which is fine if you're writing a compiler, but rather tedious otherwise. Fortunately you can just look at the order the operators are listed in the JLS, and it turns out that is the order of precedence. Java in a Nutshell shows this info in a nice table on pp. 30-31 (3rd edition). If anyone knows a good online table, please post it.

Marcela Blei
Ranch Hand
Posts: 477
I tried the following code:

And the output was: Operator precedence is: ^|
In the JLS, in section 15.22.1 the order in the title is: Integer Bitwise Operators &, ^, and |, so is the same order as the result of my testing code.
I revisted the books and I say that Eckel is putting this in a bad order, I�m going to check if it is a new version or and errata of this error or I will send and e-mail to him.