• 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

Help with -ve shift

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please help me in understanding the output below. I am trying some sample programs with shift operators.
class Test
{
public static void main(String args[])
{
System.out.println("Left by -1 = " + (8 << -1));
System.out.println("Right by -1= " + (8 >> -1));
System.out.println("Unsigned Right by -1 = " + (8 >>> -1));
System.out.println("");
System.out.println("Left by -2 = " + (8 << -2));
System.out.println("Right by -2= " + (8 >> -2));
System.out.println("Unsigned Right by -2 = " + (8 >>> -2));
System.out.println("");
System.out.println("Left by -5 = " + (8 << -5));
System.out.println("Right by -5= " + (8 >> -5));
System.out.println("Unsigned Right by -5 = " + (8 >>> -5));
}
}
Output:
Left by -1 = 0
Right by -1= 0
Unsigned Right by -1 = 0
Left by -2 = 0
Right by -2= 0
Unsigned Right by -2 = 0
Left by -5 = 1073741824
Right by -5= 0
Unsigned Right by -5 = 0

Vicky
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vicky,
I found this in the JLS at�15.19:

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.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.


According to the last part of the paragragh, by using -1 etc, you're out of range.
By the rest of the paragragh, here's my best guess at what's happening:
8 << -1;
8 will be an int, so only the rightmost 5 bits of -1 will be used
1 = 00001
1s complement = 11110
2s complement = 11111
so we have 11111 = -1
apply the & operator with a mask of 1f (or 11111)
11111
&11111
______
11111
as an unsigned number 11111 = 31.
so by "8 << -1" you're asking for a left shift of 31 places.
int 8 = 00000000 00000000 00000000 00001000 (32 bits)
Bits shifted off either end are lost, so you're left with a bunch of 0s.
You could >> a negative number, by -1, then you'd fill the entire thing with the sign bit (which is 1 in a negative number) resulting in -1.
But that would be confusing to anyone reading your code.
"8 << -1" looks like your saying "shift 8 left 1 backwards", if that's really what you want, use 8 >> 1.
Last note: You got output on 8 << -5 because -5 will resolve to 11, so the one 'on bit' didn't shifted off the int.
hth
[ August 15, 2003: Message edited by: Ray Stojonic ]
 
Vicky Nag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ray,
Thanx for the detailed explanation.
I now got the concept.
Vicky
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic