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: 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:
|
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:
And then:
Here's a complete list of operator precedence:
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. ------------------
|
| | |