• 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

bitwise >>> strange behaviour

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

when I write int i=-1;

i=i>>>31;
System.out.println(i); // i is 1 understood

when I write

int i=-1;
i=i>>>32;
System.out.println(i); // value is -1

Why so?

Regards,
Nancy
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All bitwise shift operators use modulo 32 shifting, except if the left operand is a long, then it uses modulo 64 shifting.

Simply said, if you shift your operand by 32 bits, you shift it by 32 % 32 == 0, so you don't shift it at all.

Also, on a slightly related note, you shouldn't use the >>> operator on shorts or bytes, because it can lead to unexpected results.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nancy remember that bitwise operators are not on the exam anymore...
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Stephan,

Stephan van Hulst wrote:
Also, on a slightly related note, you shouldn't use the >>> operator on shorts or bytes, because it can lead to unexpected results.



Can you please elaborate this point , I know that >>> (right shift without sign) doesn't filled left most bit with sign bit instead it just fill that with zero.

and as per my knowledge byte ans short are unsigned so eventually it fill zero only but how could it lead to unexpected result ?

Thanks
Javin

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it. What happens to operands of types byte and short before operations are carried out? They get promoted to int.

Here is an illustration. For the sake of brevity, let's say that ints are 16 bits, and shorts are 8 bits.

Nope. The short is sign extended to int before the operation:

This problem gets worse if you use an unsigned right shift compound assignment operator.
 
Javin Paul
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Stephan ,its pretty clear now.
reply
    Bookmark Topic Watch Topic
  • New Topic