Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operator precedence

 
Nancy Gu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic