Two Laptop Bag*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator precedence
 
Similar Threads
Unary Operators
bitwise operators priority--from Jimmy's mock exam
assignment operators
conditional operator precedence
Related to Boxing