Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

order of precedence in logical operators

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean val = true | false ^ true;

I don't understand why this evaluates to true.  Do the "|" and "^" have the same level of precedence?  I think the answer is yes.  So if the order of precedence is the same, then the expression is evaluated left to right?  So the statement is equivalenet to

boolean val = (true | false)  ^ true;

Which should evaluate to false.  I don't understand why the false ^ true portion of the expression is evaluated before the  true | portion of the expression.

Can someone please clarify what I am misunderstanding in this expression?
 
Rancher
Posts: 184
7
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robin Z. Clark wrote:  Do the "|" and "^" have the same level of precedence?  



No.
 
Sheriff
Posts: 26793
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or following the logical path that Robin already started down:

"If the order of precedence is the same, then the result would be false. But it isn't. So it follows that the order of precedence isn't the same."
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a nice table of operators and their precedence:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
 
Robin Z. Clark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!  That is a good table.  It is different from the table I am looking at in OCA Study Guide by Jeanne Boyarsky and Scott Selikoff on page 53.  I have not found any errors in this book so I must be misunderstanding their table.
 
Marshal
Posts: 74069
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us how that table differs rom that in Boyarsky and Selikoff.
 
Robin Z. Clark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try to include a picture.
15779625718234938210629061946752.jpg
[Thumbnail for 15779625718234938210629061946752.jpg]
 
Robin Z. Clark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the first part of the table.
15779626665284204010848945949776.jpg
[Thumbnail for 15779626665284204010848945949776.jpg]
 
Robin Z. Clark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When it says "If two operators have the same level of precedence, then Java guarantees left-to-right evaluation" I interpreted that to mean that the expression would be evaluated left to right.  Maybe what is meant is that the precedence of the operators in the table should be ordered left to right?  That seems very very confusing to me.

Operator Precedence table posted by Knute

The table that Knute referenced clearly shows that the ^ operator has higher precedence than the "|" operator.  That makes perfect sense.

I posted a topic in the book's errata thread.  Errata thread

 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you considering the parenthesis?
Whoops missed first expression w/o ()s
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic