• 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

>>> gives negative ans

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code snippet :
int x=1;
x=x>>>32;
System.out.println(x);//gives -1 why?

Mohit Agarwal
Would Be SCJP.

"The will to win is worthless if you do not have the will to prepare"
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, It will print 1 not -1.
>>> operator always returns a +ve integer. It will insert 0 at MSB starting from sign bit.

Regards,
Nandish
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me it prints 1, which is what I expected it to.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want -1, try this...

int x = 1;
x = x<<31; //shift the 1 bit all the way to the left
x = x>>31; //shift it back, filling in with 1's

But shifting an int by any operand divisible by 32 will have no effect. See...

https://coderanch.com/t/246350/java-programmer-SCJP/certification/Shift-Operation-negative-operand
[ September 30, 2004: Message edited by: marc weber ]
 
Mohit Agarwal
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am extremely sorry the initial line is:
int i=-1;
i=i>>>32;
System.out.println(i);//gives answer -1.
When ever i use multiple of 32 i got the answer -1.

Pls. clarify it.

Mohit Agarwal.
Would be SCJP.
"The will to win is worthless if u do not have the will to prepare"
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Because there are 32 bits in an int variable only five least significant bits are used from the shift amount operand. That's a shift amount of 0 to 31 bits. A 32, 64, 96, ... shift amount is taken as a 0 bit shift.
A 33, 65, 97, ... shift amount is taken as a 1 bit shift.

An n bit shift amount is always taken as n modulo 32 (for an int).

So -1 >>> 32 is -1 shifted by no bits, that is, leave it alone.

For shifting longs use 64 bits in the above explanation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic