Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interesting >>> operation

 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Sivaram.
 
Viji Bharat
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
HTH
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ).
Ajith
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic