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 Interesting >>> operation 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 "Interesting >>> operation" Watch "Interesting >>> operation" New topic

Interesting >>> operation

Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
I recently read an interesting article about the >>> operator...
int i = 3;
System.out.println(i >>> 1) ----> prints 1;
System.out.println(i >>> 2) ----> prints 0;
System.out.println(i >>> 3...31) ----> prints 0;
System.out.println(i >>> 32) ----> GUESS WHAT ---> prints 3;
HOW??? i >>> (32%32) = 3. This is based on the variable data type. If is long then i >>> 64 would print 3.
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Any Shift by 32 means no shift at all. This is because a mod 32 operation is performed on the # of bits to be shifted operator and when the # of bits to be shifted is 32, it is actually 32%32 which is 0 bits to be shifted.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Yep, rule of thumb is if the number of bits being shifted is greater than the bit-size of the data type of number being shifted, the actual shift will happen only by the result of reminder division of latter by former.
ie., if x being shifted by y bits and if y is greater than sizeof x ( number of bits, not bytes!! ), then the actual shift will occur only by y%x ( read as y mod x ).

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: Interesting >>> operation
jQuery in Action, 3rd edition