• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Booleans and Operator precedence

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does this happen?
boolean b1 = true;
boolean b2 = false;
boolean b3 = false;
b3 &= b1 | b2; // prints false...
b3 = b3 & b1 | b2; // prints true...
Operator precedence is &, ^, |
so why does the grouping change???
Is it because of the &= assignment operator? This is weird, but neat.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b2 should be true.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The first println always prints false regardless of whether b1 and/or b2 are true or false because b3 is false. false & anything is false.
[ April 24, 2003: Message edited by: Marilyn de Queiroz ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it because of the &= assignment operator?
Yes. &= has the same precendence as =, which is much lower than the precedence for & or |. So,
b3 &= b1 | b2
evaluates as
b3 &= (b1 | b2)
while
b3 = b3 & b1 | b2
evaluates as
b3 = ((b3 & b1) | b2)
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sheriffs.
I appreciate your help.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Things like this make good questions for tests, only because they have right and wrong answers and are easy to grade. I'd like to see choices in tests like:
A) This will print TRUE
B) This will print FALSE
C) Fire the programmer.
In practice, I never trust the compiler, my memory or my reader's memory, and always use parens to express my intent.
 
A teeny tiny vulgar attempt to get you to buy our stuff
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic