• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Unsigned bit shift

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody!

why does this code behave this way
public class URShift {
public static void main(String[] args) {
int i = -1;
i >>>= 10;
System.out.println(i);
//419393
long l = -1;
l >>>= 10;
System.out.println(l);
//18014...........
short s = -1;
s >>>= 10;
System.out.println(s);
//-1???
byte b = -1;
b >>>= 10;
System.out.println(b);
//-1???
}
}
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason for the -1s for short and byte would be that, prior to the right shift (with zero fill) being done, both operands would be promoted to int (if they were not already at least an int), and then after the shifting, the result would be cast back down to the type of the left-hand operand for the actual assignment. [I know that's badly worded.]
For example, short case would be equivalent to ...
short s = -1; // s is 0xFFFF
int i = s >>> 10; // i is (0xFFFFFFFF >>> 10) ... ie 0x003FFFFF
System.out.println( Integer.toHexString( i ) );
s = (short) i; // s is 0xFFFF ... ie -1
System.out.println( Integer.toHexString( s ) );
Doing a right-shift 10 on 0xFFFFFFFF would result in 0x003FFFFF. Casting this to short would leave you with 0xFFFF, which is the -1 you originally started with. Similar for byte.
Sound ok?
Greg
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,
How does this work?
int i=2;
short s=-1;
i >>= s;
System.out.println(i)?
Can u shift by negative numbers? I saw this in a mock test.
Thanks,
 
Greg Torrance
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anjo,
It seems that in this case, because the left-hand operand is int, only the first 5 bits (the least significant) of the right-hand operand are used. This ensures that the shifting will be in the range 0-31 (and effectivly strips off the high-order bit, making the right-hand operand 31, and not -1). So, you are effectively doing i >>= 31. Take a look at this (from the JDK documentation) ...
---
If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is
as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.21.1) with the mask value 0x1f. The shift distance actually
used is therefore always in the range 0 to 31, inclusive.
If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance.
It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.21.1) with the mask value 0x3f. The shift distance
actually used is therefore always in the range 0 to 63, inclusive.
---
"Can u shift by negative numbers?" I don't know how to answer that ... yes, you seemingly can shift by negative numbers, because there will be no errors, but you're not really shifting in the opposite direction - the direction of the shift will remain the same.
Hope that helps!
Greg
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic