Vicky Nag

Ranch Hand

Posts: 40

posted 13 years ago

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

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

Ray Stojonic

Ranch Hand

Posts: 326

posted 13 years ago

Hi Vicky,

I found this in the JLS at�15.19:

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 ]

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 thereforealways 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 ]