Author Topic:   Is there an easy way to remember the operatos precedence?
maha anna
bartender
posted February 14, 2000 05:33 PM             
Can someone tell me a easy way to rembember the operator precedence.. It bugs me always when I see a code like the foll.
Who will write code like this in practice...? I am always used to put ( ) to ensure what I want the computer to do and make myself and other clear at ant time.

code:

int n=7;
n <<= 3;
n = n & n + 1 | n + 2 ^ n + 3;
n >>= 2;


Assuming this code is there in a program what will be the val of n after n >>= 2; statement. How do we go about it.
Thank you.

CitySlicker
greenhorn
posted February 15, 2000 09:52 AM         
Take a look at operator precedence. You'll find that addition/mult/ are rather high and that boolean ops are low.
So here is how this code works:


int n=7;
n <<= 3;
//56 becuase 7 to binary is 111
//now push 3 zeros from behind
//111000 = 56 dec.
n = n & n + 1 | n + 2 ^ n + 3;
//addition is higher than boolean
//so n &(n +1) | (n +2) ^ (n +3)
// n & 59 | 58 ^ 59 (just start from left and go right)
// n = 57
n >>= 2;
//pushing in 2 zeros from left to right=
//111001 -> 001110 = 14

n is 14 in the end. I just kind of "memorize/visualize" op precedence.

maha anna
bartender
posted February 15, 2000 11:37 AM             
// n & 59 | 58 ^ 59 (just start from left and go right)
Step 1--> 111000 & 111011 | 111010 ^ 111011
Step 2--> 111000 | 111010 ^ 111011
Step 3--> 111010 ^ 111011
Step 4--> 000001
Gives 1 when we go from left to right. How did you get the ans diff. I think among bitwise | & ^ there is another precedence order.

Jim Yingst
sheriff
posted February 15, 2000 12:31 PM             
If you compile and run, you will see that 14 is the correct answer. The order of precedence for bitwise operators is &, ^, | - "just start from left and go right" is incorrect. Here is a corrected calculation:

n = n & n + 1 | n + 2 ^ n + 3;
n = 56 & 57 | 58 ^ 59;
n = 56 & 57 | 58 ^ 59;
n = 111000 & 111001 | 111010 ^ 111011;
n = (111000 & 111001) | 111010 ^ 111011; // evaluate &
n = 111000 | (111010 ^ 111011); // evaluate ^
n = 111000 | 000001; // evaluate |
n = 111001;

And then:

n >>= 2;
n = 111001 >> 2;
n = 1110;
n = 14;

Here's a complete list of operator precedence:


  1. ++, --, + (unary), - (unary), ~, !, and any cast
  2. *, /, %
  3. +, -
  4. <<, >>, >>>
  5. <, <=, >, >=, instanceof
  6. ==, !=
  7. &
  8. ^
  9. |
  10. &&
  11. | |
  12. ?: (ternary)
  13. =, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=

Anything listed on the same line is equal precedence - evaluate left to right.

maha anna
bartender
posted February 15, 2000 01:05 PM             
Thank you Mr.Jim. I really appreciate your help.
regds. Now I have to remember all this...which is HAAAARD.
Maha Anna.

Gilbert Baron
greenhorn
posted February 16, 2000 08:05 PM             
Look at the Bruce Eckel Thinking in Java. He has a mnemonic.
Ulcer Unary
Addicts Arithmetic
Like Logical
C Comparison
A Lot Assignment

Not perfect by any means but helpful.

I have not seen more than one or two questions on any mock that needs this other than Multiply before add and other simple ones.

------------------
"Hierro candente, batir de repente"

|