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

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: 15255
349
  • 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: 41774
887
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.
 
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic