• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

shift operators

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) public class x
2) {
3) public static void main(String[]args)
4) {
5) try
6) {
7) int i=(-1>>65535)+1;
8) int j=5/i;
9) System.out.println("end of try block");
10) }
11) catch(Exception e)
12) {
13) System.exit(0);
14) }
15) finally
16) {
17) System.out.println("finally");
18) }
}
}
Can anybody please workout on the line number 7.Hope i will get a sooner reply.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the number of positions being shifted exceeds the length of the datatype( in bytes ), the actual shift occurs only by ( numPositions % datatypeSizeInBytes ) positions.
ie.,
when you shift an integer by X positions where X is greater than 32, the actual positions shifted is not X, but ( X % 32 )
Similarly, when you shift a long by X positions where X is greater than 64, the actual positions shifted is not X, but ( X % 64 ).
Now lets look at the line
int i=(-1>>65535)+1;
Since 65535 is greater than 32, you will first have to figure out the actual number of positions for the shift. Use the formula mentioned above.
int i=(-1>>(65535%32)) + 1;
ie., int i=( -1>>31 ) + 1;
-1 >> 31 is -1 so the answer is 0.
Cheers!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
[This message has been edited by Ajith Kallambella (edited May 02, 2001).]
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nitin sharma:
1) public class x
2) {
3) public static void main(String[]args)
4) {
5) try
6) {
7) int i=(-1>>65535)+1;
8) int j=5/i;
9) System.out.println("end of try block");
10) }
11) catch(Exception e)
12) {
13) System.exit(0);
14) }
15) finally
16) {
17) System.out.println("finally");
18) }
}
}
Can anybody please workout on the line number 7.Hope i will get a sooner reply.



Hi Nitin,
Signed shift right of -1 always yield -1 regardless how many times it shifts.
- Lam
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, Ajith,
Everything you said is right, except 65535%32 should be 31
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajith,
What role does the +1 play's in finding the bit's shifted in line number 7.Please explain it.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,
Well the answer to your querry is simple it just adds 1 to the result of shifting the signed right shift >> bits of '-1'.
Please note that -1 is the only number which when <code> >> </code> shifted will always give the same result.
There is no mystery in this, the reason is very simple if you consider the fantastic bit
value of -1 which is = <code> 1111 1111 1111 1111 1111 1111 1111 1111 </code>.
  • Now let us right shift -1 by 2 bits so we have ??11 1111 1111 1111 1111 1111 1111 1111 . But in case of signed right shift <code> >> </code>. The ?? are replaced by two 1's (also known as parity bits). Thus result is 1111 1111 1111 1111 1111 1111 1111 1111 which is again -1.
    Thus if you see that in case of -1 whatever times we right shift we get the same result back.
    Cheers,
    Ravindra Mohan
    [This message has been edited by Ravindra Mohan (edited May 03, 2001).]
  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic