• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator precedence

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your question here?

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

Nick
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cheung my question is how do v evaluate the expression.
When i compiled the answer is 3 but how to arrive the answer.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramya
Here is the reply for the question you posted

int k = (((4*6)-3)/2)<<((2*5))>>>((1%2)-4)^3;
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This POST may help you.

Thanks,
Raja
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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) ....
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
osman cinar eren
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the output is 3 when u compile this program.
 
osman cinar eren
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have recently checked it; it is 20.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"[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
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

what should i understand of the precedence of casting?

regards
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks raj.... one more thing ,y are u anding -3 with 31.
[ January 28, 2005: Message edited by: Ramya JP ]
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic