Abhishek Reddy

Ranch Hand

Posts: 259

posted 10 years ago

class A

{

public static void main(String[] args)

{

test(1<<32, "1<<32");

test(1<<31, "1<<31");

test(1<<30, "1<<30");

test(1, "1");

test(0, "0");

test(-1, "-1");

}

public static void test(int i, String exp)

{

if ((i>>1)!=(i>>>1))

{

System.out.println(exp);

}

}

}

can any one help me out with the output of this program..and kindly explain me......

{

public static void main(String[] args)

{

test(1<<32, "1<<32");

test(1<<31, "1<<31");

test(1<<30, "1<<30");

test(1, "1");

test(0, "0");

test(-1, "-1");

}

public static void test(int i, String exp)

{

if ((i>>1)!=(i>>>1))

{

System.out.println(exp);

}

}

}

can any one help me out with the output of this program..and kindly explain me......

Abhishek

Swapna Omprakash

Greenhorn

Posts: 1

posted 10 years ago

This program prints 1 << 31

-1

The range of int primitive is -2^31 to (2^31) - 1.

and so (1 << 31) does not fit in an int and results in a negative number.

where as due to the masking of bits,

when the value to be shifted is an int,

for the shift values of >=32, the actual size of the shift is the value of the right-hand operand masked by 31.

ie.the shift distance is always between 0 and 31.

if shift value is > 32 shift is value%32

So here, 1 << 32 = 1.

Ex:

16 << 35 => 16 << (35 % 32) => 16 << 3 = 128

16 << 32 => 16 << (32 % 32) => 16 << 0 = 16

16 >> 35 => 16 >> (35 % 32) = 2

16 >> 32 => 16 >> (32 % 32) = 16

similarly for longs

for the shift values of >=64, the actual size of the shift is the value of the right-hand operand masked by 63.

ie.the shift distance is always between 0 and 63.

if shift value is >= 64 shift is value%64

And this question tests one more concept,

in unsigned right shift operation,

if the left-hand operand is negative, the result is equivalent to the left-hand operand right-shifted by the number indicated by the right-hand operand plus two left-shifted by the inverted value of the right-hand operand.

So here -1 >>> 1 = (-1 >> 1) + (2 << ~1) = 2147483647.

and since -1 >> 1 = -1 they both are not equal.

Ex: -16 >>> 2 = (-16 >> 2 ) + ( 2 << ~2 )

---------------------------------------------

Swapna.

-1

The range of int primitive is -2^31 to (2^31) - 1.

and so (1 << 31) does not fit in an int and results in a negative number.

where as due to the masking of bits,

when the value to be shifted is an int,

for the shift values of >=32, the actual size of the shift is the value of the right-hand operand masked by 31.

ie.the shift distance is always between 0 and 31.

if shift value is > 32 shift is value%32

So here, 1 << 32 = 1.

Ex:

16 << 35 => 16 << (35 % 32) => 16 << 3 = 128

16 << 32 => 16 << (32 % 32) => 16 << 0 = 16

16 >> 35 => 16 >> (35 % 32) = 2

16 >> 32 => 16 >> (32 % 32) = 16

similarly for longs

for the shift values of >=64, the actual size of the shift is the value of the right-hand operand masked by 63.

ie.the shift distance is always between 0 and 63.

if shift value is >= 64 shift is value%64

And this question tests one more concept,

in unsigned right shift operation,

if the left-hand operand is negative, the result is equivalent to the left-hand operand right-shifted by the number indicated by the right-hand operand plus two left-shifted by the inverted value of the right-hand operand.

So here -1 >>> 1 = (-1 >> 1) + (2 << ~1) = 2147483647.

and since -1 >> 1 = -1 they both are not equal.

Ex: -16 >>> 2 = (-16 >> 2 ) + ( 2 << ~2 )

---------------------------------------------

Swapna.

It is sorta covered in the JavaRanch Style Guide. |