• 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

calculation question

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I sticked with this little one for a while:

int i = 4*6-3/2<<2*5>>>1%2-4^3;
System.out.println(i);

The answer is: 3

I try to do:
i=(24-3/2)<<(10)>>>(1%2-4)^3=23<<10>>>(-3)^3

then what's next?

3/2=1 and 1%2=1 right?

Can anybody give more detail info for next couple of calculation steps?

Thanks
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

the operator precedence for the operators available in java can be found at the following link:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

From the table shown in the above link,

we can classify the precedences of the operators specific to the given expression "i = 4*6-3/2<<2*5>>>1%2-4^3" as follows:-

Note:- For simplicity, we assign numbers to the set of operators

Precedence Operator
---------- --------
1 * / %
2 -
3 << >>>
4 ^
5 =

Thus,
The given expression gets executed as:

Step1: * / % are executed
i = 24 - 1.5 << 10 >>> 1 - 4 ^ 3
Step2: - is executed
i = 22 << 10 >>> -3 ^ 3 // notice here that 22.5(24 - 1.5) becomes 22
Step3: << >>> are executed
i = 22528 >>> -3 ^ 3
i = 0 ^ 3 // when operators of equal precedence occur,left to right
// execution occurs. this idea can also be understood from
// the associativity property of each operator used in java
Step4:
i = 3
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ramchandra!

Sorry! I still confuse a little bit. Can you give more detail explain?

what rules applied on this : "22.5(24 - 1.5) becomes 22" and for 3/2 I think the division between integers, the result should get integer right?

how can get this done: "22 << 10 get 22528"
"22528 >>> -3 get 0"
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already figured out : "22 << 10 get 22528"
But still confuse on "22.5(24 - 1.5) becomes 22" and "22528 >>> -3 get 0".

Can anyone help it out?
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still stick with this question. Need help!
Thanks.
 
ramchandra sugasi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

the '<<' operator can be applied on integer arguments only.
thus,
The integer value of 22.5 is rounded off to 22.

'>>>' operator takes negative integer value which results
in a zero.

hence,
the answer is 3.
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Sorry I am so fool.

But I put "(24-3/2)" in my compiler. Whatever I declare the results types like:

int x=(24-3/2); or
float x=(24-3/2);or
double x=(24-3/2);

The output prints are 23 or 23.0 but never are 22.5 or 22. That's why I'm still confusion. Sorry I am so fool.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Do we need to know these things for SCJP 5? I think it has been removed from the objectives for SCJP 5.
Even K&B says that.
pls let me know if I am wrong.
Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic