• 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

Interesting >>> operation

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic