• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sybex CSG 17 - Error in the Appendix (Chapter 2: Operators, Answer #13)

 
Greenhorn
Posts: 1
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Appendix (Chapter 2: Operators, Answer #13) says:

"The first expression is evaluated from left to right since the operator precedence of & and ^ is the same, letting us reduce it to false ^ sunday, which is true, because sunday is true."

However, the & operator has higher precedence than the ^ operator and not the same one. That's why the statement

"boolean goingToTheStore = sunny & raining ^ sunday;"

evaluates "sunny & raining" first andit can be reduced to false ^ sunday.

Evidence:
System.out.println(true ^ true & false); // true
System.out.println((true ^ true) & false); // false
If the operators & and ^ have the same precedence and the statement "true ^ true & false" is evaluated from left to right, the result would be false and not true.

Am I right?
Peter Prazenica
 
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, & has higher precedence than ^.
 
author & internet detective
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Confirmed and added to the erratas crediting you. Good find. This was also wrong in our Java 11 books. Have a cow for finding an errata none of our Java 11 readers caught either!

Table 2.1 on page 68 correctly lists them as differing levels of precedence.
 
Let's get him boys! We'll make him read this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic