This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence 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 "Operator precedence" Watch "Operator precedence" New topic
Author

Operator precedence

Nancy Gu
Greenhorn

Joined: Dec 31, 2000
Posts: 6
Hello, everybody:
What is the eveluating order and results for these questions?
Pls expain it for me. Thanks.

----------------------------------------
int n = 7;
n<<=3;<br /> n = n & n + 1 | n + 2 ^ n + 3;<br /> n >>= 2;
System.out.println(n);
----------------------------------------
8 | 9 & 10 ^ 11
----------------------------------------
boolean b1=b2=b3=b4=true
b1 | b2 & b3 ^ b4
----------------------------------------
boolean b1=b2=b3=b4=true
b4 = b4 | b1 & b2;
b3 = b3 & b1 | b2;
Amit, Jhalani
Greenhorn

Joined: Jan 11, 2001
Posts: 21
int n = 7;
n<<=3;<br /> n = n & n + 1 | n + 2 ^ n + 3;<br /> n >>= 2;
System.out.println(n);
The order of execution for the bitwise and or and Xor operator is as follows first it evaluates the bitwise and(&) then it evaluates the bitwise Xor(^) ann then it evaluates the bitwise or(|)
but in the expression(n = n & n + 1 | n + 2 ^ n + 3 )) all the airthmetic operators will be evaluated first then the bitwise operators and finally the assignment took place.
so the result is 56.
8 | 9 & 10 ^ 11
result will be 14.
boolean b1=b2=b3=b4=true
b1 | b2 & b3 ^ b4
you can not do assignment and initialization at the same time.
rather you can say
boolean b1,b2,b3,b4;
b1=b2=b3=b4=true;
b1 | b2 & b3 ^ b4;
then result would be true.
& is first to be evaluated then ^ and then |
same resion for the following.
b4 = b4 | b1 & b2;
b3 = b3 & b1 | b2;
correct me if i am wrong.
Thanks.
[This message has been edited by Amit, Jhalani (edited January 12, 2001).]
[This message has been edited by Amit, Jhalani (edited January 12, 2001).]
 
GeeCON Prague 2014
 
subject: Operator precedence