aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about & and | Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about & and |" Watch "Question about & and |" New topic
Author

Question about & and |

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
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.
May Pat
Ranch Hand

Joined: Jul 01, 2002
Posts: 32
It prints "3".


May P.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jul 30, 2001
Posts: 149
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?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
& and | are not short-circuit operators. The expressions on both sides of the operator are executed, always.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jul 30, 2001
Posts: 149
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about & and |