Please explain how to evaluate the value of variable a.
I am able to understand the evaluation of variable b and c but a is still confusing for me.
This is a question from Dan's site.
(Corrected [C0DE][/C0DE] bracketing) [ August 01, 2004: Message edited by: Barry Gaunt ]
SCJP 1.4, SCWCD 1.4
Joined: Jul 30, 2004
Regd the code evaluation for a I struggled at first as well.. so I tried it out on the compiler and I saw the output 3, 0, 3 After trying different bracket combinations, it looks like this is what the expression a is really like
int a = 1 | (2 ^ (3 & 5));
So 3 (0011) & 5 (0101) = 1 (0001), followed by 2 (0010) ^ 1 (0001) = 3 (0011), followed by 1 (0001) | 3 (0011) = 3 (0011)
I just wanted to consult an advice, are we expected to remember the order or preecedence for the exam, which in this case looks like & then ^ then |.
Can someone suggest !!.
Peter den Haan
Joined: Apr 20, 2000
Originally posted by Vikramaditya Bindal: [...] are we expected to remember the order or preecedence for the exam [...]
- Peter [ July 31, 2004: Message edited by: Peter den Haan ]
Joined: May 11, 2004
Thanks for your replies...Vikramaditya and Peter.
Actually dan has given this explanation for this answer.
"Java evaluates operands from left to right while respecting operator precedence. The order of operator precedence starting with the lowest is as follows: |, ^, &. Although complete memorization of the operator precedence chart is not necessary, it is a good idea to memorize the three levels that appear in this question."
And he has written the "lowest" word, which was a little confusing and I could not understand how to reach this answer.