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 |.
- 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.