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

Question about & and |

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain about & and |. Can some one run through the following example.

public class ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
System.out.println(i);
}
}
Thanks.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints "3".
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suresh, how ya doing?
Notice that the name of the class ShortCkt seems to imply short circuit logical operands are around.
But the operator & applied to boolean operands will always evaluate both operands.
Does that help to move on a little with the problem?
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those second operands to the & operators have side-effects resulting in the value of i being updated.
It's the new value of i that is printed, the value of the boolean b is not interesting.
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Barry
Thanks for asking. How are you doing?
int i = 0;
boolean t = true;
boolean f = false; b;
1. b = (t & ((i++) == 0));
b = (true & (1 == 0)) that is the reason it will not execute above statement?
2. b = (f & ((i+=2) > 0));
b = (false & ( 2 > 0)) even this is not true why is it displaying output as 3.
Is there any chart we have to remember about short circuits? like we have for exclusive OR (^) and (&) and (|).
Thanks
Why will it not execute statement 1?
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
& and | are not short-circuit operators. The expressions on both sides of the operator are executed, always.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look at part 1. The t & ((i++) == 0) part.
& is a binary "and" operator being used on t, on the left; and ((i++) == 0) on the right. t has the value true. Let's find out the value of the righthand side.
Notice the i++. That's a postfix ++ operator.
That means you have to use the value of i before it is incremented. That value is 0. So the righthand side is the result of 0 == 0, that is true.
So the result of part 1: true & true is assigned to b. Now true & true results in true. Any other combination is false.
But there is something else that happens, a side-effect. The side-effect is that i becomes 1.
After the part 1 the value of i is 1. We do not care about the value of b.
The important thing to see is that both sides of the & operator had to be evaluated.
If you can follow all that and understand it, then try part 2 yourself.
-Barry
[ August 16, 2002: Message edited by: Barry Gaunt ]
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that i value is carried from previous statement where i = 1 then from barrys explanation, for the below statement
b = (f & ((i+=2) > 0));
b = (f & ((3) > 0)) which is
b = (false & true)
My question is on what basis it will print 3. Is it going to print on false and true assumption? That is the reason output is 3?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Suresh, you have got that i gets changed to 3, by the i+=2. You also have that b = false & true which is false. But the value of b is not needed. The program prints the new value of i, which is 3!
How does it look?
-Barry
BTW I go to sleep now.
reply
    Bookmark Topic Watch Topic
  • New Topic