File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dans Question -Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dans Question -Operators" Watch "Dans Question -Operators" New topic

Dans Question -Operators

shetal bansal
Ranch Hand

Joined: May 09, 2005
Posts: 63

What is the result of attempting to compile and run the program?
Answer is 31,0.
Can anyone explain me the exact pen and paper calculation for the same.As I feel the calculation will be pretty hectic, considering that int is 32 bit.
Henry Wong

Joined: Sep 28, 2004
Posts: 20531

Hope this helps,
[ May 18, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Note: for shifting, only the lowest five bits are used

And in case of long, use lowest six bits.
shetal bansal
Ranch Hand

Joined: May 09, 2005
Posts: 63
thanxs a lot for the nice explanation!!
Just wanted to confirm one thing, that in case of integers, for shifting lowest 5 bits are used and in case of shifting of long, lowest 6 bits are used?
Are these the only two cases?Meaning no other special cases where the no. of bits vary?
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
Those are the only possiblities.All other types byte,short,char are promoted to int.

The type promotion is applied to both the operators separately.
The resulting type and number of bits shifted depends only on the type of the L.H.S operator.
Hope this helps....
[ May 19, 2005: Message edited by: vinuharan haran ]
I agree. Here's the link:
subject: Dans Question -Operators
It's not a secret anymore!