aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Operator precedence" Watch "Operator precedence" New topic
Author

Operator precedence

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
public class Question48{
public static void main(String[] args) {
int i = 4*6-3/2<<2*5>>>1%2-4^3;
System.out.println(i);
}
}

Answer plssss
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
What is your question here?

It is easy that you put it to the compiler and you can get the result!

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
cheung my question is how do v evaluate the expression.
When i compiled the answer is 3 but how to arrive the answer.
komanduru saritha
Greenhorn

Joined: Jan 24, 2005
Posts: 1
Ramya
Here is the reply for the question you posted

int k = (((4*6)-3)/2)<<((2*5))>>>((1%2)-4)^3;
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi,

This POST may help you.

Thanks,
Raja


SCJP 1.4
Murali Atmakuri
Greenhorn

Joined: Dec 13, 2004
Posts: 4
Ramya ,
The evaluation order goes this way
(((4*6)-(3/2))<<(2*5)>>>((1%2)-4))^3
One point to remember is that the result of shift operator is always zero if the shift distance is negative


JCP 1.4 ,SCWCD (Preparing)
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
"One point to remember is that the result of shift operator is always zero if the shift distance is negative "

this statement is actually wrong. try with Integer.MIN_VALUE(which is equal to the shift with 0), Integer.MIN_VALUE+1 (which is equal to shift with 1) ....


SCJP/SCWCD
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
can anybody help me further



1.4*6=24
24-(3/2)=23

2.2*5=10

3.((1%2)-4)=-3

(23<<(10>>>-3))^3

i am correct until this.......
If so how do i proceed further.
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
(23<<(10>>>-3))^3
actually the shift operand on the left should be MOD 32.
(so my writing above for the min value is incomplete)then;

-3--> -3+32=29
10>>>29=0

23<<0=23

23^3;

23=10111
3 =00011

23^3=10100=20
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8712
Attention, Attention...

This is the "Focus Police" !

This is all very interesting, but just for the sake of those of you who are studying for either the SCJP 1.4 or the new Tiger cert...

RELAX !

This is NOT on the exam! I know it's on a lot of the mock exams you can find, but it's NOT on the real exam...

HTH


Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
Do you mean the operator precedence is not worth studying as it is not in the exam?

we should not work on those table that show the operator precedence?

or is it just the negative right hand operator?
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
but the output is 3 when u compile this program.
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
i have recently checked it; it is 20.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8712
What I'm saying is that complex operator precedence is NOT on either exam... some simple stuff might be (like the precedence of casting), but none of this complicated spaghetti code stuff you see on the mocks...

I'm not saying don't study it, (I wouldn't, but you can ), I'm just saying it's not on the exam
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"[On precedence of bit operations: ] I know it's on a lot of the mock exams you can find, but it's NOT on the real exam..."

I was glad that the SCJP did not test this. If the SCJP tested this, the test would become much more of test of trivia than test of practical experience.

As a practical matter, practicing programmers normally know the precedence table in its broad scope. For example, we know that . (the dot operator) binds tighter than the logical operators, which bind tighter than arithmetic operators, which in turn bind tighter than the assignment operators.

I had a program manager who used to say "multiplication and division before addition and subtraction, and everything else goes in parenthesis."

I think he was being partly facetious. Professional programmers would find annoying reading code that put in parens operators that are commonly used and several tiers of precedence apart. The less commonly used operators (e.g. bit twiddlers) should have parens to increase readability and maintainability.

(BTW, I don't parenthesize expressions with && and ||. The operations are so common in boolean expressions [e.g if( a || b && c)] that I believe programmers should just know that && has a higher precedence than ||. I also think they should be fair game on the SCJP.)
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
hi,

what should i understand of the precedence of casting?

regards
Abi Raj
Greenhorn

Joined: Jan 28, 2005
Posts: 28
hi Ramya,

This is how it works

1) (4*6)-(3/2)<<(2*5)>>>(1%2)-4^3

2) (24-1)<<10>>>(1-4)^3

3) ((23<<10)>>>-3)^3

23 << 10 = 23552 // Each left shift corresponds to multiplication of the value by 2. so 23 * 2 power 10 is 23552

4) (23552 >>> -3) ^3

23552 >>> -3

the value of right hand operand is -3 is ANDed with the mask 11111 (i.e 31), giving the shift distance of 29.

-3 & 31
1111 1111 1111 1111 1111 1111 1111 1101 & 0000 0000 0000 0000 0000 0000 0001 1111

29

23552 >>> 29

0000 0000 0000 0000 0101 1100 0000 0000 >>> 0000 0000 0000 0000 0000 0000 0001 1101

0000 0000 0000 0000 0000 0000 0000 0000

0

5) (0 ^ 3)

0000 ^ 0011

0011

3

so the answer is 3

Hope this helps
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
thanks raj.... one more thing ,y are u anding -3 with 31.
[ January 28, 2005: Message edited by: Ramya JP ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator precedence
 
Similar Threads
operator
Question about String split() ... (was DOUBT)
JSP fragments correctly define a method
whts your answer
define a method in JSP