File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j  running order? 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 "i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j  running order?" Watch "i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j  running order?" New topic
Author

i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j running order?

Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
This question is from Dan Chisholm's mock.
class I {
public static void main (String s[]) {
int i = 5;
int j = 10;
System.out.println(i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 0
b. Prints: 5
c. Prints: 10
d. Prints: 15
e. Prints: true
f. Prints: false
g. Runtime Exception
h. Compiler Error
i. None of the Above
I was wondering how does this run " i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j "? What's the order?
Where can I find good tutorials about this? I feel NOT confident in this field.
Thanks a million!
Moya
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
To be really brutal, try the Java Language Specification.
Otherwise, look at the tutorials to be found at the Sun Java Website
or here on JavaRanch.
-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.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html has an operator precedence table.
There must also be one somewhere in the JLS, but I don't know where. I hope someone else can post a link.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
janus place
Greenhorn

Joined: Aug 14, 2002
Posts: 8
The answer is (h) : Code does not compile.
-Janus
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j
Java evaluates expressions from left to right. The first expression to be evaluated is (i & j). The result is zero. The next expression to be evaluated is the "or" operation.
0 | (((i ^ j) ^ ((i >> 1) == j)) ? i & j : i ^ j)
The left operand is zero and the right operand is the rest of the expression. Java will continue to evaluate the rest of the expression until an operator is encountered that has equal or lower precedence than the "or" operator.
Java will evaluate the expression that is the right operand of the "or" operator.
((i ^ j) ^ ((i >> 1) == j))
The first "exclusive or" operation on the left (i ^ j) is evaluated first. The result is a binary value of 1111 of type "int", and it becomes the left operand of the next "exclusive or" operation. The right operand of the second "exclusive-or" operation is as follows.
((i >> 1) == j)
The result of ((i >> 1) == j) is a boolean value of false.
Java then tries to evaluate the "exclusive or" operation
(1111 ^ false)
The result is a compiler error.
[ August 15, 2002: Message edited by: Dan Chisholm ]

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
why is i &j equal to 0?


SCJP 1.4
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
i & j is equal to 00000101 & 00001010 which is 0.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
After reading Dan's explanation, I think Janus cheated . Dan, how on earth do you get a handle on such a complex expression?
Here's where I show my ignorance. Doesn't the compiler scan from left to right like this?

Oh! I'm going to go and get some paper and the JLS to try this out.
Be back right soon now, still editing this post!
Ok, all that up there is wrong. For more find my last post down there, and move to Marcus Green's place.
-Barry
[ August 15, 2002: Message edited by: Barry Gaunt ]
[ August 16, 2002: Message edited by: Barry Gaunt ]
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
i'm sorry i still cannot get it
how did you come up with 0? can anybody please explain further ?
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Barry,
Java does indeed evaluate expressions from left to right, but it also respects precedence. Evaluate the following statement.
a = 1 + 2 * 3;
Is the answer 7 or is it 9?
Here's an interesting exercise. See if you can write a description of how Java evaluates the above. Remember, java does indeed evalute the expression from left to right in addition to respecting precedence.
After you have written an explanation for the above expression evaluation, then apply the same technique to the code presented earlier in the thread.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Dan:
What is the probability that a similar question
will be found in real exam (1.4). If I can avoid
studying such topics, I want to...
Thanks
Barkat
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Maria Garcia:
i'm sorry i still cannot get it
how did you come up with 0? can anybody please explain further ?

int i = 0101;
int j = 1010;
Starting from the left most bit, 0 & 1 = 0. Now the second bit from the left, 1 & 0 = 0. Now the third bit from the left, 0 & 1 = 0. Now the bit on the far right, 1 & 0 = 0.
Here's the table for the AND operation
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Suppose you have binaries like i = 00001010 and j = 00000101. The & operator is used column by column

0 and 0 returns 0
0 and 1 returns 0
1 and 0 returns 0
1 and 1 returns 1
so

Another example,

[ August 15, 2002: Message edited by: Anthony Villanueva ]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Barkat Mardhani:
Hi Dan:
What is the probability that a similar question
will be found in real exam (1.4). If I can avoid
studying such topics, I want to...
Thanks
Barkat

You will certainly run into one or more questions that deal with operators on the real exam. Furthermore, it will be difficult to avoid encountering the use of operators while working as a programmer. However, the question that we have been discussing is very difficult. Have you completed the "Operators Exam" on the "Topic Exam" page of my mock exam?
I've been thinking about removing that difficult question and replacing it with a few easier questions.
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Thank you so much, everyone! I get it.
Special thanks to Ron for the link to the precedence table and Dan for the step-by-step explanation.
Moya
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ron wrote:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html has an operator precedence table.
There must also be one somewhere in the JLS, but I don't know where. I hope someone else can post a link.

Ron, I could not find one in the JLS, a table that is. You have to carefully read JLS section 15 and "build" it up yourself. I did that and I'm happy to report that I got the same as your link gives.
Dan, now I know that bitwise exclusive-or (^) comes before bitwise-or (|) I can make headway. I find these "guess what this code does" examples fairly difficult, and therefore never write such convoluted code myself. If in doubt "stick brackets around" has always been rule I follow. In this case the analysis is harder than the synthesis.
Moya, I'm happy you've got it, hope to catch up with you soon.
[ August 16, 2002: Message edited by: Barry Gaunt ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
One thing that bothers me about test questions like this is that they are asking you what some piece of code does that you would never want to write in real life.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
On the remote chance that anyone is interested in my journey to expression-enlightenment I have moved it over here Marcus Green/Discussions/JavaCertifiedProgrammer/Expression Analysis (from JR) - an example.
-Barry
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Ron Newman:
One thing that bothers me about test questions like this is that they are asking you what some piece of code does that you would never want to write in real life.

You are correct. When I write an arithmetic expression "in real life" I try to keep it short and I use a lot of parenthesis so that the order of evaluation is obvious. However, the purpose of the exam is to verify that a person fully understands the purpose of each operator, the precedence of each operator, and the order of evaluation. Even so, I agree that the exam question that we have been discussing here is too difficult and should be replaced with a few easier questions. I'll do that later today.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Ron Newman:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html has an operator precedence table.
There must also be one somewhere in the JLS, but I don't know where. I hope someone else can post a link.

Ron,
Thank you for this link. I'll put it on my answer pages.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Even so, I agree that the exam question that we have been discussing here is too difficult and should be replaced with a few easier questions. I'll do that later today.

No! leave it there Dan, please. I am just honing back the edge for solving such problems. At least leave it in a "Supplementary Questions" section or something. They make one dig down below the surface and develop true understanding. I was going to ask for more like it.
-Barry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j running order?