• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

operator precedence

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would like to be sure. On one mock test, if found this as explanation of a question:
since (b1|b2&b3^b4) evaluates as (b1 | (b2&(b3^b4))) then ...
Is this correct??
I thought because of operator precedence & ^ |
(b1|b2&b3^b4) evaluates as (b1|((b2&b3)^b4)))
Am I wrong?
Thanks for your response,
Cyril.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I thought because of operator precedence & ^ |
(b1|b2&b3^b4) evaluates as (b1|((b2&b3)^b4)))



sorry, must be changed in:
(b1|b2&b3^b4) evaluates as (b1|((b2&b3)^b4))
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realize I'm new to javaranch, so please tell me if I'm missing something:
These are all bitwise operators. Doesn't java evaluate them left-to-right, as in:
((b1|b2)&b3)^b4) ?
Note example:
class BenOps {
public static void main(String []args){
int i1=1;
int i2=2;
int i3=4;
System.out.println(i1|i2);//3
System.out.println(i2&i3);//0
System.out.println(i1|i2&i3);//1
System.out.println((i1|i2)&i3);//0
System.out.println(i1|(i2&i3));//1
}
}
If it helps others, this is a little mnemonic I devised to help me remember the precendence:
UnArShComBiShConAss
Unary
Arithmetic
Shift
Comparison
Bitwise
Short Circuit (|| &&)
Conditional (?boolean a:b)
Assignment
Respectfully,
Ben
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charles,


These are all bitwise operators. Doesn't java evaluate them left-to-right


I'm not sure at all. Look at this very simple example:
class Test{
public static void main(String arg[]){
boolean b1 = true;
boolean b2 = true;
boolean b3 = false;
System.out.println(b1^b2&b3);//1
}
}
Output: true.
So b1^b2&b3 can not be equivalent to (b1^b2)&b3 = false&false = false
(you can test it by changing line 1 : add convenient parenthesis)
Instead, it clearly looks to be similar to b1^(b2&b3) = true^(false) = true
Cyril.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what the question is.
The precedence of the bitwise and logical operators is
&
^
|
They do not have equal precedence. They do not group from left to right.
Precedence rules specify that certain operators, in the absence of parentheses, group "more tightly" than other operators.
Since operator precedence is the "stickiness" of operators relative to each other,
(b1|b2&b3^b4) is equivalent to (b1 | ((b2 & b3) ^ b4))
[ August 03, 2003: Message edited by: Marlene Miller ]
 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, Marlene. Nothing is in any of my java books (Thinking in Java and the Heller/Roberts Study Guide)about that deeper level of "stickiness". Thanks. Where are you getting your info?
Ben
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both Roberts/Heller and Eckel do talk about this, though perhaps not enough to make it clear for a newbie. Look for the sections that talk about "precedence", right where they first mention operators. Or see this in Sun's tutorial:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
Precedence refers to the order in which operators are grouped - it determines whether
a + b * c
should be viewed as
(a + b) * c
or
a + (b * c)
(it's the latter). However, once you've made those decisions, each individual operator will still be evaluated from left to right. (a, then b, then c.) Precedence doesnt' change that - but it governs what you do with the results. (After evaluating a, b, and c, do you add a to b and then multiply by c, or do you multiply b and c and then add to a?) Here, it may not matter what order you evaluate a, b, and c. But if they were method calls, or had other side effects like a ++ operator, then the order of evaluation would matter.
Hope that helps...
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charles.

Where are you getting your info?


The Java Language Specification - for a correct description of the language
The Java Programming Language - for understanding and insights.
The way the JLS Section 15.22 Bitwise and Logical Operators specifies operator precedence is a little strange.
The Java Programming Language Section 6.10 lists the operators on separate lines according to precedence. I learned the "stickiness" metaphor from them.
Marlene
[ August 03, 2003: Message edited by: Marlene Miller ]
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thanks Jim and Marlene for your responses.
So, what we can say:
1�) operator precedence has nothing to do with evaluation order
2�) & ^ | have not the same precedence
3�) as I guessed it, explanations of the concerned Mock's exam are also false.
Thanks again,
Cyril.
 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, all.
Yes, Jim. You are right. I went back and looked again. It's in there, but just not explained well with good examples. However, it is explained well in that tutorial. That's a nice tutorial. (Maybe I should have just studied that, instead of buying all these books for the exam. :-)
By the way, that tutorial also pretty fully explains my other question on this forum, regarding the meaning of the word "expression". (See "Expression/Operator Question")...
and "statement"...
and "operator", for that matter. Once you understand the definitions fully, you can apply them and the logic then becomes pretty clear.
Ben
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

operator precedence has nothing to do with evaluation order


Yes, precedence tells us where to add parentheses. Then we have to apply the rule: evaluate the left operand before the right operand.


It turns out, the operands are always evaluated from left to right, but the operators are not always performed in order of precedence.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic